Development

Git Command Cheat sheet

In this article, you will get a list of useful commands when working with git version control. This will allow you to quickly manage your repositories in quick steps.
Captain Salem 3 min read
Git Command Cheat sheet

Creating Repositories

Command Operation
git init [name] Create new local repository.
git clone [url] Clone repository.
git clone [url][path] Clone a repository to target directory.

Git Configuration Commands

Command Operation
git config --global user.name "username" Sets the name used in git commits and tags.
git config --global user.email "email" Sets the email addressed attached to the commits and tags.
git config --global --edit Edits the git global configuration in a text editor mode.
git config --global color.ui auto Allows colorization of the git output.

Git Branches Commands

Command Operation
git branch List local branches.
git branch -av List both local and remote branches.
git checkout [branch_name] Switch to the branch with the defined name and update the working directory.
git chechout -b [branch_name] Create a new branch with the specified name.
git branch -d [branch_name] Delete branch with the specified name.
git checkout [branch_1] && git merge [branch_2] Merge branch_1 into branch_2.
git branch -m [new_name] Rename a branch to the specified name
git tag [tag_name] Tag the current commit.
git push origin -u [name] Push and reset.
git push origin --delete [old] Delete specified remote branch.
git branch -vv List branches and upstreams (verbose).
git checkout - Switch to previous branch.
git checkout [branch] -- [file] Checkout single file from another branch.

Git Operations

Command Operation
git status Show modified files, staged for next commit.
git add [file] Stage a specified file.
git add . Stage all modified files and directories.
git commit -m "message" Commit the staged files with the specified commit message.
git commit -am "message" Commit all tracked files.
git reset [file] Unstage the specified file and reserve file changes.
git reset --hard Reset everything to last commit.
git diff Diff of changed but unstaged files.
git diff --staged Diff of staged but uncommited files.
git commint -v --amend Rewrite last commit message.

Git Synchronisation

Command Operation
git pull Fetch and merge commits from remote repository.
git fetch [alias] Fetch all the branches from remote repo.
git push [alias][branch] Upload local branch commits to remote repo branch.
git merge [alias]/[branch] Merge remote branch into current branch
git merge --no-ff [alias]/[branch] No fast forward.
git cherry-pick [commit_id] Merge specific commit from another branch to your current branch.
git merge --ff-only [alias]/[branch] Only fast forward.

.gitignore Patterns

# don't ignore
![file]

# ignore mac files
.DS_store

# ignore node modules
node_modules

*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

File Tracking

Command Operation
git rm [file] Delete file from staging area.
git mv [old_path] [new_path] Change a file path and stage the new file path.
git log --stat -M Show all commit logs with path moves.

Git Remote Operations

Command Operation
git remote add [alias][url] Add git URL as an alias.
git remote Show names of remote repositories.
git remote -v Show names and URL of remote repositories.
git remote rm [repo_name] Remove remote repo.
git remote set-url origin [URL] Change URL of git repo.

Stash Operations

Command Operation
git stash Save modified and staged files.
git stash drop Discard changes from top of stash stack.
git stash pop Write working from top of stash stack.
git stash list List stack-order of stashed files.

Observe Repo Commands

Command Operation
git log Show commit history for current branch.
git log brach1..brach2 Show commits on branch1 that are missing from branch2.
git log --follow [file] Show commits responsible for changes in a given file.
git show [SHA] Show object in Git human-readable format.
git diff brach1...branch2 Show difference of brach1 and branch2.

Git Aliasing

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

Termination

Thanks for tuning in to our cheatsheets and articles. You can grab a PDF copy of this cheatsheet in the resource below.

If you enjoy our content, please consider buying us a coffee to support our work:

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.