Useful git commands with examples

Here it gives some useful commands with examples. These commands cover most of the usage scenarios in your daily use of git. With them, your experience with git will becomes much more easier.

Here are commands covered in this article.
– git config
– Config common used command alias
– Config editorConfig default commit template
– Config Beyond Compare 4 as your difftool
– Add .gitignore to ignore files
– git stash
– git reset
– git revert
– git cherry-pick
– git diff
– git difftoo
– git format-patch
– git show
– git log
– git grep

git config

git config can be used to get and set options in a repository or globally. Below are several useful examples to set your repository.

Config common used command alias

You can config some alias of commands to make them simpler to type.

# Use alias "git co" instead of "git checkout"
$ git config --global alias.co checkout

# Use alias "git st" instead of "git status"
$ git config --global alias.st status

# Set "git last" as a alias to view the last commit
$ git config --global alias.last 'log -1 --oneline HEAD'

# Set "git unstage" as a alias to undo the changes in index
$ git config --global alias.unstage 'reset HEAD --'

Config editor

# Config vim as your editor
$ git config --global core.editor vim

# Config notepad++ as your editor
$ git config --global core.editor "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

See more tools as text editor in git.

Config default commit template

Step 1: write your own git commit template file. Such as file ~/.gitmessage and put below contents:

Subject line (try to keep under 50 characters)

Multi-line description of commit to explain
what, why and how this change is being made
(try to limit each line under 72 Characters)

Provide ticket numbers or links to other relevant resource
Example:
[Ticket: #53]

Step 2: config it as your commit template:

# Config a custom commit template
$ git config --global commit.template ~/.gitmessage

See setting commit template for how to write ignoring rules.

Config Beyond Compare 4 as your difftool

# Config Beyond Compare 4 as difftool

$ git config --global diff.tool bc4
$ git config --global difftool.prompt false
# Note: set parameters for Beyond Compare tool
$ git config --global difftool.bc4.cmd '"C:Program FilesBeyond Compare 4BCompare.exe" "$LOCAL" "$REMOTE"'  

Add .gitignore to ignore files

In fact, setting up files to ignore does not use git config command.

To set up your.gitignore file:

Step 1: create a file named .gitignore in the root of your git repository. Put your ignoring rule in the file, below is an example:

# ignore all .class files
*.class

# ignore all .log files
*.log

# ignore build folder in the root directory
/build/

Step 2: commit .gitignore to make it take effect:

$ git add .gitignore
$ git commit

See ignoring files in git for more.

git stash

git stash can be used to save changes away temporally (it store them on a stack) if you don’t want to commit yet. It is very useful when you need to pause the task in progress and checkout to another branch for a more emergent one. Below is your work flow with git stash:

# Stash your work on current branch, let's call it develop
$ git stash

# Checkout to topic branch for the emergent task
$ git checkout topic

# After you finish your work, execute add and commit 
$ git add .
$ git commit

# Checkout back to the prior branch
$ git checkout develop

# Apply changes you have stashed before
$ git stash apply

# Continue your work on develop branch

Other examples:

# Apply the top stash on the stack
# Note: staged changes in the stash are not restaged 
# and stash is removed from stack.
$ git stash apply stash@{0}

# Revert working tree and staging area
$ git stash apply --index

# Repplay a stash and remove it from stack
$ git stash pop stash@{0}

See stashing changes for more usages.

git reset

git reset can be used to reset current HEAD to point to another commit. By using different options, you can control the reset degree, such as commit, index, working tree.

Examples:

# Discard the last commit. 
# The commit's modifications in working tree are kept.
$ git reset HEAD~

# Discard the last commit.
# The commit's modifications in staging area are kept.
$ git reset --soft HEAD~

# Discard the last commit. 
# The commit's modifications in working tree are not kept.
$ git reset --hard HEAD~

git revert

git revert can be used to revert a commit that has been pushed to remote repository. This command does not change the history but makes a new commit which reverses the modifications.

# Undo a commit that has been pushed. 
$ git revert -m 1 HEAD

See more about undoing operations.

git cherry-pick

git cherry-pick applies changes introduced in some specified commits. With this command, you can make a new commit with the changes got from another commit :

Examples:

# Apply the changes introduced by commit 95f2137 and
# create a new commit with the change
$ git cherry-pick 95f2137

# Apply the changes introduced by tip commit of develop branch
$ git cherry-pick develop

# Apply the changes introduced by tip commit of develop branch 
# in working tree and staging area, but not commit the changes.
$ git cherry-pick --no-commit develop

See more examples on copying changes from another commit.

git diff

git diff shows difference between commits, staging area and working tree. Its output can also be used to generate a patch.

Examples:

# Difference between HEAD's parent and HEAD
$ git diff HEAD~ HEAD

# Difference between HEAD's parent and HEAD for index.php
$ git diff HEAD~ HEAD -- ./index.php

# Difference between working tree and HEAD
$ git diff HEAD

# Difference between staging area and HEAD
$ git diff --cached HEAD

# Create a patch from difference
$ git diff HEAD > patch-issue-53.patch

# Apply a patch generated by git diff output
$ git apply -v atch-issue-53.patch

See showing differences for more examples.

git difftool

git difftool shows difference in a visual tool (see set beycond compare as your difftool). It accepts the same options with git diff.

Examples:

# Difference between HEAD's parent and HEAD
$ git difftool HEAD~ HEAD

# Difference of index.php between working tree and HEAD
# Note: if there is no difference, nothing is output.
$ git difftool HEAD~ HEAD -- index.php

See showing differences for more examples.

git format-patch

git format-patch generates patches containing submit information which can be used to email submission.

# Create patchs for commits since <commit> to the current branch tip commit
# Note: the specified commit is not included
$ git format-patch <commit>

Examples:

# Create a patch for the commit HEAD
$ git format-patch HEAD~

# Create patches for commits HEAD~2, HEAD~, HEAD
$ git format-patch HEAD~3

# Create patches for a range of commits: HEAD~ and HEAD
# Note: it prepares each commit with its patch in one file per commit.
git format-patch HEAD~2..HEAD

# Apply a patch created using format-patch
$ git am 0001.patch

git show

git show can be used to show content of objects like a commit, a stash, etc.

Examples:

# Show contents of asset/css/style.css in commit HEAD.
$ git show HEAD:asset/css/style.css

# Show changed files in commit HEAD
$ git show --name-only HEAD 

# Show content of the top stash
$ git stash show stash@{0}

git log

git log can be used to view and search the commit history.

Exmples:

# Show commit history in short format with one log in one line
$ git log --oneline

# Show only the last two commit logs
$ git log -2

# Show commit logs that are made after 2018-12-01
$ git log --after=2018-12-01

# Show only those commits that add or remove string of "update_post_meta"
$ git log -S update_post_meta

# Show only commits made by john
$ git log --author john

# Show all commits of branches in a graph structure
$ git log --all --graph --oneline --decorate

See more about viewing git history.

git grep

git grep is highly useful when you need to do a search in your git repository.

Examples:

# Grep "Device is HD capable" in .xml files of current directory
$ git grep 'Device is HD capable' -- './*.xml'

Tips: By adding limit arguments (like path and file extensions), you can highly reduce the running time of git grep command.