git branch

This post lists some useful Git commands related to branching and merging. remote is used as a generic placeholder for remote name below. It may be origin. branch is used for branch name.

List all branches

git branch -v

List all remote branches

git branch -r -v

See status log with branch refs

git log --decorate=full

Pull changes in specified remote branch, merge and commit into current branch

git pull remote branch

Fetch changes from remote branch into FETCH_HEAD, but continue on current branch

git fetch remote branch

Switch to FETCH_HEAD branch

git checkout FETCH_HEAD

Create a new branch with specified name from current start point

git checkout -b branch

Switch to master branch of origin

git checkout master

Merge and commit changes in specified branch into the current branch

git merge remote/branch

Merge changes in specified branch, result available in working tree to commit (history is not preserved)

git merge --squash remote/branch

Run your favorite merge tool to resolve conflicts (I use meld, on Mac OS X)

git mergetool -t meld

Diff of file with version on HEAD (or branch name)

git diff [--cached] HEAD file

Delete a local branch

git branch -d branch

Delete remote branch in cloned repo (can restore from origin with pull)

git branch -d -r remote/branch

Permanently delete branch ref from remote (use caution)

git push remote --delete branch
## OR
git push remote :branch

Create a new branch at current start point, but continue on current branch

git branch branch

Checkout and switch to a branch, or start tracking and switch to a remote branch with same name

git checkout branch

Push branch to remote (add -f to force update)

git push [-f] remote branch

Push all branches to remote

git push --all remote

Track the specified remote branch as upstream

git branch -u remote/branch

Reset to previous commit on current branch

git reset HEAD~

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s