A Mutable Log

A blog by Devendra Tewari


Project maintained by tewarid Hosted on GitHub Pages — Theme by mattgraham

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 main branch of origin

git checkout main

Merge and commit changes in specified branch into the current branch

git merge remote/branch

Merge changes in specified branch, without preserving history, result available in working tree to commit

git merge --squash remote/branch

Merge changes in specified branch, favoring their changes, without preserving history, result available in working tree to commit

git merge -s recursive -X theirs --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~

Reset current branch to commit on remote tracking branch

git reset --hard remote/branch