Skip to content

Instantly share code, notes, and snippets.

@gwierink
Last active May 23, 2016 12:55
Show Gist options
  • Save gwierink/72648b94bf218de60736ef3ed2a32224 to your computer and use it in GitHub Desktop.
Save gwierink/72648b94bf218de60736ef3ed2a32224 to your computer and use it in GitHub Desktop.
Git notes

Git notes

What?

These are just a bunch of notes on git commands that I regularly need, but not so regularly that I never need to look them up. To prevent weekly digging in stack overflow, I created this gist. Mainly for myself, but great if useful for anyone else as well.

Compiling Git

Assuming you have git-core compile the latest version of Git by:

# Get Git, I prefer having a directory "Tools" in my home:
$ mkdir ~/Tools
$ cd Tools
$ git clone git://git.kernel.org/pub/scm/git/git.git

Make sure you have the necessary dependencies (from git-scm, with addition of autoconf, tested on Ubuntu-15.10):

$ sudo apt-get install \
      libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev \
      asciidoc xmlto docbook2x autoconf

Now change to the git directory and compile:

$ cd ~/Tools/git
$ make configure
$ ./configure --prefix=/usr
$ make all doc info
$ sudo make install install-doc install-html install-info

Source: git-scm

Using git

Pull -- rebase to checkout a remote branch without merge commit

Sometimes I need to checkout someone else's branch, fix some stuff locally, and push it back up for review. Pulling the remote branch works fine, but the method below creates a merge commit that IMHO is not necessary:

# Create and change to remote branch "some_fix"
$ git checkout -b some_fix

# Fetch and merge in changes to some_fix
$ git pull origin some_fix

A fast-forward pull on the local branch does not always work:

$ git pull --ff-only origin some_fix

The best solution I found so far is to make pull use rebase:

# Create and change to remote branch "some_fix"
$ git checkout -b some_fix

# Fetch and merge in changes to some_fix
$ git pull --rebase origin some_fix

Prune to clean up a remote branch no longer needed

To work on a remote branch I pull the branch, do some local work, push, and open a pull request. Pull request accepted, remote branch on GitHub deleted, all fine.

# Create and change to remote branch "some_fix"
$ git checkout -b some_fix

# Fetch and merge in changes to some_fix
$ git pull --rebase origin some_fix

# Work, work, work. Then, commit and push to remote
$ git add someFile.C
$ git commit -m "Improved function blabla"
$ git push origin some_fix

Suppose some_fix was merged into master via a pull request and that the branch on GitHub has been deleted. Then, on my local machine the branch, objects, and refs still exist:

$ git branch -a
  master
* fix/some_fix
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/fix/some_fix

Changing to master, pulling in the newly merged changes, and deleting some_fix resolves this partly:

# Change to master
$ git checkout master
Switched to branch 'master'

# Pull in the merged changes
$ git pull origin master

# Throw away the local branch some_fix. Use -D instead of --delete, because the
# changes in some_fix have not yet been merged, according to the local
# repository.
$ git branch -D some_fix

Now master is up-to-date, some_fix is deleted on the remote, but the latter still exists as a remote branch on the local machine. This shows up as the "remotes/fix/some_fix" branch:

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/fix/some_fix

Using fetch --prune gets rid of this clutter:

$ git fetch --prune
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

Sources: http://stackoverflow.com/questions/20106712/what-are-the-differences-between-git-remote-prune-git-prune-git-fetch-prune

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment