Home > Applications & Tools > Commonly Used Git Commands With Samples

Commonly Used Git Commands With Samples

January 27th, 2015 Leave a comment Go to comments

Git is a powerful version control system. One big differentiator from traditional version control systems is that it’s fully ditributed. In other words, there is no central repository and everyone can have a full clone of everything.

There are many articles and tutorials about GIT already. A while back, I also wrote a few articles on the Java APIs to the JGit implementation. This post is simply a collection of some commands that I use on daily basis. This is just for my quick reference or cheat sheet and nothing more. If you find it’s helpful, it’s great.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

# Set the user name so that no need to type it
git config --global user.name "Steve Jin"
 
# Set the author email address
$ git config --global user.email steve@example.com
 
# Configure the default editor to be emacs. Another popular choice is vi or vim.
git config --global core.editor emacs
 
# Configure the template for commit message
git config --global commit.template ~/.gitmessage.txt
 
# Clone a repository
$ git clone http://example.com/doublecloud/vijava.git
 
# Create a new repository
$ git init
 
# Create a new branch "fix123" and check out from origin/master branch into it.
$ git checkout -b fix123 origin/master
 
# Switch from current branch to branch fix321
$ git checkout fix321
 
# Add all the changed file into staging. Alternatively, one or file paths can be added.
$ git add .
 
# Add one specific file named doublecloud.py into staging for later commit. More files can be added in one line.
$ git add doublecloud.py
 
# Check the current status of this branch: what's staged, what's changed but not staged
$ git status
 
# Commit the staged changes into repository
$ git commit
 
# Update the latest commit with new changes. The commit number will be different.
$ git commit --amend
 
# Show what is inside a commit
$ git show 8f79c86c7b089d5583c653f1e879e41f2ff0084f
 
# Fetch changes from remote
$ git fetch
 
# Rebase the current branch with its tracking branch, which should be done before push to remote.
$ git rebase
 
# Pull changes from remote and merge them. Not used as often as git fetch + git rebase
$ git pull
 
# Push the changes from local fix123 branch to the remote master branch
git push origin fix123:master
 
# Show the commit history of current branch
git log
 
# Show the commit history with one line for each commit
$ git log --oneline
 
# Show what's new in local branch fix123 that has not yet pushed to origin/master branch
$ git log origin/master..fix123
 
# Show short version of git log
$ git shortlog
 
# List all the branches with # indicate the current branch
$ git branch
 
# List more info about branches with tracking branches
$ git branch -vv
 
# Set the tracking branch as origin/master for the branch fix123
$ git branch --set-upstream fix123 origin/master
 
# Delete the branch fix123. Use -d switch for safer checking
$ git branch -D fix123
 
# Edit the history of a few commits
$ git rebase -i origin/master
 
# Show the diff between working directory and HEAD
$ git diff
 
# Show the set of files changed in a commit
$ git diff-tree --name-only -r HEAD
 
# Show the diff of same file but different versions on two branches
$ git diff branch1 branch2 -- doublecloud.py
 
# Pick a specific commit and apply it on top of current branch
$ git cherry-pick 259df0b6100b507df9c665128cff0159999d5b4c
 
# Clean the current working directory. Remove --dry-run when you have doublechecked
$ git clean -d -f --dry-run
 
# Move a file or directory from one location to another
$ git mv doublecloud.properties conf/
 
# Add a new tag called v1.0 
$ git tag -a v1.0 -m 'First public release'
 
# List tags
$ git tag -l
 
# Check out a tagged version v1.0 into a new branch
$ git checkout -b fix1.0 v1.0
 
# See change history of a list per line. The 2nd shows only line 1 to 5.
$ git blame <file-name>
$ git blame -L 1,5 <file-name>
 
# Remove un-tracked files
$ git clean -f
 
# Change the tracking branch to origin/master
$ git branch --track [new_branch] origin/master
 
# Combine last 2 commit - need to pick commands in editor
$ git rebase -i HEAD~2
Categories: Applications & Tools Tags: ,
  1. No comments yet.
  1. No trackbacks yet.