Git Commands (Every day use)

Here are the list of git commands that I use every day:

  • Clone remote repository
  • list the branches in test-repo repository.
    • git branch -vv
  • Switch between branches
    • git checkout milestone1
    • git checkout milestone2
  • Create a new branch work1 from milestone1
    • git checkout milestone1
    • git branch work1
    • git checkout work1
    • You can combine above two commands with “git checkout -b work1”
  • Push new branch work1 to repository test-repo
    • git push origin
  • Merging branches (merge milestone2 code to work1)
    • git checkout milestone2
    • git pull (git fetch & git merge origin/milestone2)
    • git checkout work1
    • git pull
    • git merge –no-ff milestone2
    • git push origin work1
  • push a modification to file1 to work1 branch
    • git chekcout work1
    • git pull
    • git add file1
    • git commit -m “message” (or git commit -a -m “message” to skip staging area)
    • git push origin work1
  • Delete local branch
    • git branch -d work1 (to delete tracking branch)
    • git push origin  :work1 (to delete branch on remote repository)
  • You have done some changes in your local tracking branch, but you want to work on different branch and come back. If you have new file, then add it to the index.
    • git add newfile
    • Do changes in current indexed file file1.
    • git stash
    • git checkout master
    • do some work on master branch
    • git checkout work1
    • git stash pop (will pop both file1 and newfiles changes on top of work1 branch)
  • See the history of commits
    • git log
    • git log -p -1
    • git log –stat
  • Code differences
    • git diff origin/master (local uncommitted changes with remote tracking branch)
    • git diff master (local uncommitted changes with tracking branch, where you commits your changes)
    • git diff COMMIT^ COMMIT (compare commit with ancestor commit of tracking branch)
    • git diff –staged (staged changes with tracking branch recent commit)
    • git diff (not committed)
    • git diff HEAD (upstaged changes with recent commit)
    • git show (to see the commited changes, difference with parent commit)
    • git diff milestone1..milestone2 (difference between two branches)
  • Status of your changes
    • git status
  • Resetting local committed changes
    • git reset <commit-id> (HEAD is moved to the specified commit-id)
  • Resetting local staged changes
    • git rm –cached
  • Discard changes in current working directory. (not staged)
    • git checkout — file1
  • Bringing changes to exiting commit
    • git add file2
    • git commit –amend (will add file2 change into same commit, not pushed)
  • Generate patch from commit 
    • git format-patch -n1
    • git am <p (apply a specific patch)

We need to understand some theory behind how git works. Some common terms we come across while working with git:

Repository, Remote tracking branch,  Tracking branch, Local branch

Repository – A set of branches related to project. (git clone <repository link>)

Remote tracking branch –

Remote-tracking branches are references to the state of remote branches. They’re local references that you can’t move; they’re moved automatically for you whenever you do any network communication. Remote-tracking branches act as bookmarks to remind you where the branches in your remote repositories were the last time you connected to them.

On my computer origin/master is the remote tracking branch which refers to the branch on hosting computer. When I do git pull, git push,  the git command uses origin/master info to talk to hosting computer(

Tracking branch /Local branch – This is the branch on my computer, which will have all the commits I have done but not yet pushed to the remote branch.

git branch -vv
  ganga     8e22413 [origin/ganga: ahead 2] integration of allocation logic
  master    4ae3a25 [origin/master] db change
* bug1 c8676f9 [dev/bugs: ahead 4, behind 2] fix for illegal option
  fet1   62a363b container changes

In above example,  Local branch or tracking branches are : ganga, master, bug1 and testing

Remote tracking branches : origin/ganga, origin/master, dev/bugs,

Tracking branches are local branches that have a direct relationship to a remote branch. If you’re on a tracking branch and type git pull, Git automatically knows which server to fetch from and branch to merge into

Git index (aka staging area/cache/directory cache/staged files) – Changes that are added for commit.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s