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~

Specify a different ssh key for each host

The ~/.ssh/config file can be edited to specify a different key/identity for each host. This is useful when you have different ssh keys setup on different git servers.

Host mycompany
    User fooey
    IdentityFile ~/.ssh/github.key

If you specify a HostName that is different from Host, the .git/config file should use the name specified in Host. That should also be the host name used in git commands such as clone and remote.

See Simplify Your Life With an SSH Config File for more.

History of file in Git repo

Who changed what, and when, in a file in a Git repo? The following simple command is usually enough

git log -p path

git stash

git stash can be a useful mechanism to set aside work you’re not ready to commit yet.

To stash staged changes simply run

git stash

Files in working directory not added to index are not affected, unless you add the -u option to stash those as well.

To view a list of stashes

git stash list

To inspect details of latest stash

git stash show stash@{0}

0 is the id shown by stash list. Use of stash@{0} is optional for latest stash entry.

To apply changes in latest stash to working directory

git stash apply

To eliminate the latest stash (use pop to apply and drop)

git stash drop

If stash apply or pop fails due to conflicts, but you still want to stage the changes, try

git checkout stash -- .

-- indicates current branch. The single dot indicates path beginning at current directory, but may be several different path specs. stash may be replaced with stash@{0}, use a different id to work with older stashes.

git tag

This post summarizes some useful tag related commands. All commands assume a tag with value v1.0.0, a version number. To create an annotated tag

git tag -a v1.0.0 -m "a comment"

To list all tags

git tag

To view details and diff for a tag

git show v1.0.0

To delete a tag

git tag -d v1.0.0

To replace a tag when it already exists

git tag -a v1.0.0 -f -m "a comment"

To create a tag pointing to the same commit as another tag

git tag -a v1.0.0 v1.0.0-rc -m "a comment"

To create a tag pointing to a specific commit

git tag -a v1.0.0 commit -m "a comment"

To checkout working tree at tag (add option -b to create branch at that point in the tree)

git checkout v1.0.0

To view commit log with tags

git log --decorate=full

To push tags to origin (add -f to force update if tag exists on remote)

git push origin --tags

To push tag deletes to origin

git push origin :refs/tags/v1.0.0

For other commands and options use man git or man git-tag.

git archive

To quickly get a source distribution in zip format

git archive --format=zip -o HEAD source/ README

Assuming, source code is under the folder source. README is a file in the same folder as source. Subfolders ignored by .gitignore are not archived. You can replace HEAD with a commit label. zip can be replaced with tar or tar.gz. See man git-archive for more details.

Working with multiple git remotes

This post discusses useful git commands when working with multiple git remotes such as to share code with distributed teams.

We are all familiar with the clone command

git clone repository_url

This creates a local working copy of the remote, identified by the short name origin. You can see all the remote repositories in your working copy thus

git remote -v

The following command fetches commits from origin

git pull

The following command pushes local commits to origin

git push

What if you want to share your copy of the repository so that another team can work on it? Maybe to a new remote repository shared with that team?

This can be done as follows

git remote add repo_short_name repository_url
git push repo_short_name master

You can pull changes from the new repository

git pull repo_short_name master

And push them to origin (origin is assumed if not specified)

git push origin

If a remote has conflicting commits that you want to favor

git pull -s recursive -X theirs repo_short_name master

Pull and rebase on top of remote

git pull --rebase repo_short_name master

For more information on working with remote repositories see chapter 2 of the free Git book.

Once you have mastered the command line, you can do all of the same things using a Git client such as the free SourceTree app from the folks at Atlassian.