Git Cheat Sheet
Git Basics
To create an empty git repo in current directory.
git init
To create an empty git repository in specified directory.
git init [directory]
To set author name to be used for all commits.
git config --global user.name "Your Name"
To set author email to be used for all commits.
git config --global user.email "you@example.com"
This will enable colorization of Git output.
git config --global color.ui auto
To clone repo located at given url onto local machine via HTTP or SSH protocol.
git clone [repourl]
It will show the status of your working directory. Options include new, staged, and modified files. It will retrieve branch name, current commit identifier, and changes pending commit.
git status
Stages the change of a specific file.
git add [file]
Stages all changes in the current directory for the next commit.
git add .
Commit the staged snapshot, with "message" as the commit message.
git commit -m "message"
To rewrite last commit message.
git commit --amend -m "new message"
To commit all your tracked files to versioned history.
git commit -am "commit message"
Git Log
To display the entire commit history using the default format. (Press 'q' to exit out of log)
git log
To display the full diff of each commit in current branch.
git log -p
To limit number of commits by [limit].
git log -[limit]
To display each commit in a Git repository's history on a single line.
git log --oneline
To displays statistics for the number of files changed and lines added or deleted for each commit.
git log --stat
To search for commits by a particular author.
git log --author= [pattern]
To search for a commits with a commit message that matches "pattern" string.
git log --grep= "pattern"
To display commits that have the specified.
file.git log -- [file]
To draw a text based graph of commits on left side of commit messages.
git log --graph
To add names of branches or tags of commits.
git log --decorate
To combine multiple flag in single command.
git log --oneline --decorate --graph
To show the commits on branchA that are not on branchB.
git log branchB..branchA
To show the commits that changed file, even across renames.
git log --follow [filename]
Git Diff
To show the unstaged changes between your index and working directory.
git diff
To show difference between working directory and last commit.
git diff HEAD
To show difference between staged changes and last commit.
git diff --cached
To show the diff of what is in branchA that is not in branchB.
git diff branchB...branchA
Git Remote
To list names of the remote repositories set.
git remote
To list names and URLs of the remote repositories.
git remote -v
To create a new connection to a remote repo. You can use [name] as a shortcut for [url] in future commands.
git remote add [name] [url]
To fetch a specific [branch], from the repo. By excluding [branch], you can fetch all remote refs.
git fetch [remote] [branch]
To pull the specified remote's copy into the local copy.
git pull [remote]
To push the branch to [remote]. It will create [branch] in the remote repo if it doesn't exist.
git push [remote] [branch]
To push all of your local branches to the specified remote.
git push [remote] --all
To remove a remote repository.
git remote rm [remote_repo]
To change the URL of the git repo.
git remote set-url origin [git_url]
Git Reset
To reset staging area to match most recent commit.
git reset
To unstage a file while retaining the changes in working directory.
git reset [file]
To reset staging area and working directory to match most recent commit and overwrites all changes in the working directory.
git reset --hard
Move the current branch tip backward to [commit], reset the staging area to match, but leave the working directory alone.
git reset [commit]
Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after [commit].
git reset --hard [commit]
Git Branching
To list all local branches in repository.
git branch
To list all branches and their upstreams.
git branch -vv
To list all branches both local and remote.
git branch -a
To create a new branch if does not exist.
git branch [branchName]
To create a new branch if does not exist and will check out.
git checkout -b [branch]
To switch to a branch.
git checkout [branch]
To switch to previous branch.
git checkout -
To merge [branch] into the current branch.
git merge [branch]
To delete given branch.
git branch -d [branchName]
To rename current branch name to new_name.
git branch -m [new_name]
To rename the remote branch name.
git push origin -u [new_name]
To fetch the remote branches.
git branch -r
To delete remote branch.
git push -d [remote_name] [branchname]
Git Stashing
To put current changes in your working directory into stash for later use.
git stash
To list stack-order of stashed file changes.
git stash list
To apply stored stash content into working directory, and clear stash.
git stash pop
To delete a specific stash from all your previous stashes.
git stash drop
To see the stash, if you have ore stashes.
git stash show -p
To see the last stash in detail.
git stash show 1
To apply right stash
git stash apply 1
Tracking Path Changes
To delete the file from project and stage the removal for commit.
git rm [file]
To change an existing file path and stage the move.
git mv [existing-path] [new-path]
To show all commit logs with indication of any paths that moved.
git log --stat -M
Ignoring Files
To ignore all files from logs directory
/logs/*
To ignore all files with extension ".log"
*.log
To instruct git to not to ignore .gitkeep file.
!logs/.gitkeep
File to be created with name to include ignore patterns.
.gitignore
To create system wide ignore pattern for all local repositories.
git config --global core.excludesfile [file]
Git Tags
To list all tags.
git tag
To create a tag reference named name for current commit.
git tag [name] [commit sha]
To create a tag object named name for current commit.
git tag -a [name] [commit sha]
Remove a tag from local repository.
git tag -d [name]
The tags flag is used to send all of your local tags to the remote repo. Tags are not automatically pushed when you push a branch.
git push
Git Aliases
To create shortcut for a Git command.
git config --global alias. [alias-name] [git-command]
To set git logron as alias.
git config --global alias.logron "log --graph --oneline"
To set git st as alias.
git config --global alias.st status
Tracking Path Chages
To delete the file from project and stage the removal for commit.
git rm [file]
To change an existing file path and stage the move.
git mv [existing-path] [new-path]
To show all commit logs with indication of any paths that moved.
git log --stat -M