Thursday, June 5, 2014

GIT: common commands

Checking Status - what has changed

$ git status

Determine the URL of the remote REPO

git config --get remote.origin.url

Show differences

$ git diff HEAD FETCH_HEAD

Fetching remote changes

$ git fetch

Rebasing - placing your changes on top of remote changes

$ git rebase

Branch Operations

Switch to an existing branch

$ git checkout develop

Create a new branch named thrift

$ git checkout -b thrift 

Delete an existing branch

$ git branch -D thrift

Resetting - removing local changes

reset hard to previous commit

$ git reset --hard 9a5xxx317

Get local code to the state that is on remote server

Sometimes you may do local changes that you may not like, or your coworker does changes to the same code that are better than yours. In that case you need to fetch remote change, reset your state to what is on remote server and then clean up.

$ git status
$ git fetch
$ git reset --hard HEAD
$ git clean -f -d 
$ git status
On branch master
Your branch is behind 'origin/master' by 4 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working directory clean

$ git rebase
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.