Skip to content

Instantly share code, notes, and snippets.

@matthieua
Last active March 9, 2025 11:25
Show Gist options
  • Save matthieua/2840043 to your computer and use it in GitHub Desktop.
Save matthieua/2840043 to your computer and use it in GitHub Desktop.
GIT: Cheatsheet
# add all tracked files that have been changed.
git add -u
# choose which changes to commit.
git add -p path/to/file
# interactive mode
git add -i path/to/file
# delete a branch
git branch -d branchname (if merged)
git branch -D branchname (if unmerged, force delete)
# display remote branch
git branch -r
# display all branches
git branch -a
# display merged branches
git branch --merged
# display non merged branches
git branch --no-merged
# display branches that contain a particular commit.
git branch --contains <commit id>
# rename branch
git branch -m old_branch new_branch
# See which changes from a branch are already present upstream
# while on "feature" branch:
git cherry -v master
# Merge a single commit from another branch into your local one.
git cherry-pick some commit ID
## remote branch
git push origin :branchname
# Fetch changes from a remote repository.
git fetch <remote name>
# View the log with one shortened commit ID and subject.
git log --oneline
# View the last N commits.
git log -5
# Limit the log output to a single file or directory.
git log filename
# View the commits in the last week.
git log --since="1 week"
git log --since="168 hours"
git log --before="1 week"
git log --until="7 days"
# View the log entries by a single commiter
git log --author="some user"
# Search for a commit using regex
git log --grep="some regex" -i
# Show statistics about commits in the log.
git log --stat
git log --shortstat
# Show branches, tags in git log
git log --oneline --decorate
# shows the last commit which has the word "fix" in its message
git show :/fix
# update the gitignore file
git rm -r --cached .
# Display file with entire line-by-line commit information.
git blame some/file
# Start the output of blame at line 10.
git blame -L 10 some/file
# Limit the output of blame to lines 10 through 20.
git blame -L 10,20 some/file
git blame -L 10,+11 some/file
# point a remote branch to another branch
git push origin from-branchname:to-branch-name
# revert a merge (m1 means the first parent)
git revert 3b6e7a4bb3db78ed7b27eba79ed7ffec5b30156e -m1
# View the differences between the staged changes and repository.
git diff --staged
# Differences with specific commit
git diff Commit ID
# Difference between 2 commits
git diff 423d021 1e85ac3 [filename]
# Seeing stats between 2 commits
git diff --stat 423d021 1e85ac3 [filename]
git diff --shortstat 423d021 1e85ac3 [filename]
# Diff by highlighting inline word changes instead of whole lines
git diff --word-diff
# Diff between branches
git diff --name-status master..branch
git diff --stat master..branch
# This removes everything from the index, then just run:
git add .
git commit -m ".gitignore is now working"
# generate SSH Key
cd ~/.ssh
ssh-keygen -t rsa -C "$mail"
# Short status output
git status -sb
# display list of tags
git tag
# tag the latest commit
git tag v1.0
# view avalaible tags
git tag -l
# deleting a tag name
git tag -d tag_name
# deploy on remote server
http://toroid.org/ams/git-website-howto
git diff master branchname
git merge branchname
# merge without a commit
git merge --no-commit branchname
# force merge with a commit
git merge --no-ff branchname
# merge with a commit message
git merge -m "my message" development
# delete a branch
git branch -d branchname
# delete a branch that hasn't been merged
git branch -D branchname
# solve conflict with mergetool
git mergetool
# Set gvimdiff as the default merge tool.
git config --global merge.tool "gvimdiff"
# Create a shallow clone with the last fifty commits.
git clone --depth 50 some-repository
# unstage stages files
git reset HEAD filename
# Fix the previous commit by removing it entirely.
git reset --hard HEAD
# save current work without committing it
git stash "message"
# stash list
git stash list
# reapplying stash
git stash apply | pop
# show specific stash changes
git stash show stash@{0}
# delete a specific stasg
git stash drop stash@{0}
# delete stash
git stash clear
# git bisect
git bisect start
git bisect good fd0a623
git bisect bad 256d850
git bisect good
git bisect bad
# current version
git rev-parse HEAD
# commiters
git shortlog -n -s
git log -p FILE
To view changes in history for README.md, for example, type
> git log -p README.md
git log -S’PATTERN’
To search for changes that matches the pattern “stupid” in history, for example, type
> git log -S'stupid'
git add -p
To interactively stage and unstage changes, type
> git add -p
git remove –cached FILE
This command removes remote file copy only. For example, typing
> git remove --cached database.yml
removes database.yml that is already checked in but leaving the local copy untouched. This is intensively handy for removing ignored files that are already pushed without removing the local copies.
git log ..BRANCH
This command returns all the commits in a branch that are not in HEAD. For example, assuming you are on a feature branch, typing
> git log ..master
gets all commits that are in master but not merged into the current feature branch yet.
git branch –merged & git branch –no-merged
This command returns a list of branches that are merged or not yet merged to current branch. It’s a useful check before any merging happens. For example, on a featrue branch, typing
> git branch --no-merged
returns a list of branchs that are not yet merged to the feature branch.
git branch –contains SHA
This returns which branch contains a specified sha key, for example, typing
> git branch --contains 2f8e2b
shows all branches containing the commit 2f832b. It is very helpful for verifyng whether a git cherry-pick is done correctly for instance.
git status -s
It returns a less verbose version of git status. I setup this command as my default when running git status to reduece noise.
git reflog
It shows a list of operations you have done to your local git repository. It’s very helpful for restoring lost commits for example since it returns the complete history of all git operations with commit SHAs.
git shortlog -sn
This shows a list of contributors ordered by number of commits. Similar to the contributors view of GitHub.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment