GitCheatsheet

Git

Git Cheat Sheet English

Index


A text search on all files in the directory:

$ git grep "Hello"

Show commits that introduced a specific keyword

$ git log -S 'keyword'

Show commits that introduced a specific keyword (using a regular expression)

$ git log -S 'keyword' --pickaxe-regex

Commit History

Show all commits, starting with newest (it'll show the hash, author information, date of commit and title of the commit):

Show changes over time for a specific file:

Display commits that are present only in remote/branch in right side

Who changed, what and when in <file>:


Move / Rename

Rename a file:

Rename Index.txt to Index.html


Branches & Tags

List all local branches:

List local/remote branches

List all remote branches:

Switch HEAD branch:

Checkout single file from different branch

Create and switch new branch:

Switch to the previous branch, without saying the name explicitly:

Create a new branch from an exiting branch and switch to new branch:

Checkout and create a new branch from existing commit

Create a new branch based on your current HEAD:

Create a new tracking branch based on a remote branch:

Delete a local branch:

Rename current branch to new branch name

Force delete a local branch:

You will lose unmerged changes!

Apply specific commit from another branch:

$ git tag

$ git tag -n

$ git remote -v

$ git remote show

$ git remote add

$ git remote rename <new_remote>

$ git remote rm

$ git fetch

$ git remote pull

$ git pull origin master

$ git pull --rebase

$ git push

$ git push : (since Git v1.5.0)

$ git push --delete (since Git v1.7.0)

$ git push --tags

Use your configured merge tool to solve conflicts:

Merge & Rebase

Merge branch into your current HEAD:

List merged branches

Rebase your current HEAD onto <branch>:

Don't rebase published commit!

Abort a rebase:

Continue a rebase after resolving conflicts:

Use your editor to manually solve conflicts and (after resolving) mark file as resolved:

Squashing commits:

Now replace this,

to this,


Undo

Discard all local changes in your working directory:

Get all the files out of the staging area(i.e. undo the last git add):

Discard local changes in a specific file:

Revert a commit (by producing a new commit with contrary changes):

Reset your HEAD pointer to a previous commit and discard all changes since then:

Reset your HEAD pointer to a remote branch current state.

Reset your HEAD pointer to a previous commit and preserve all changes as unstaged changes:

Reset your HEAD pointer to a previous commit and preserve uncommitted local changes:

Remove files that were accidentally committed before they were added to .gitignore


Last updated

Was this helpful?