Git notes

From Noah.org
Jump to: navigation, search


Contents

git notes

Kernel.org git QuickStart

Show what changed when printing the log (diff/patch view)

You want to see the actual lines of code changed with the log. This is useful for searching for code that may no longer exist in the current revision.

git log -p

show previous revisions of a file

This shows the current version of a given file in the current directory.

git show HEAD~0:./FILENAME

This shows the previous version of a given file in the current directory.

git show HEAD~1:./FILENAME

diff version before last commit

If you already did a commit, this will show you what that commit changed.

git diff HEAD~1 FILENAME

High level view of all changes, including merges

git log --oneline --graph --all

pretty printing upstream tracked branches

# git for-each-ref --format='%(refname:short) -up-> %(upstream:short)' refs/heads | column -t
develop           -up->  origin/develop
master            -up->
release-tag1      -up->  origin/release-tag1
release-tag2      -up->  origin/release-tag2

checkout remote tracked branch

git fetch -a
git checkout develop 

set upstream branch for local branch that has no tracking

You ran git branch release-tag2, but didn't specify an upstream tracking branch. To fix this, first list your branches and the associated upstream branch to see where you are, then use git branch again to set the upstream tracking branch; finally, list your branches again to see that it is now tracking upstream.

# git for-each-ref --format='%(refname:short): %(upstream:short)' refs/heads
develop: origin/develop
master:
release-tag1: origin/release-tag1
release-tag2:

# git branch -u origin/release-tag2 release-tag2
Branch release-tag2 set up to track remote branch release-tag2 from origin by rebasing.

# git for-each-ref --format='%(refname:short): %(upstream:short)' refs/heads
develop: origin/develop
master:
release-tag1: origin/release-sprint49
release-tag2: origin/release-tag2

pull and push errors when working in a clone of an empty repository

If you are getting errors like No refs in common and none specified, failed to push some refs, and Your configuration specifies to merge with the ref 'master' from the remote, but no such ref was fetched. then the fix is easy (run git push origin master). The reason this happens is because your cloned repository is so new that is doesn't even have branch definitions.

$ git pull
Your configuration specifies to merge with the ref 'master'
from the remote, but no such ref was fetched.
$ git push
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to 'git@git.noah.org:dotfiles'

The solution is to simply run this command:

$ git push origin master

Search for deleted files in version history (search for all the times the file was changed)

A long time ago someone removed or moved a file. You want to go back to that old version to research the code.

git log -- rootfs/etc/skel/.bashrc

You can also search by the leading directory path even though git doesn't think about directories. For example, search for when anything from rootfs/etc/skel was deleted:

git log -- rootfs/etc/skel/.bashrc

git checkout old revision by refid or date.

This will show you the refids of revisions in a date range. Then you can pick a revision and check it out.

git log --since='2013-12-01' --until='2013-12-25'
git checkout ff009a rootfs/etc/skel/.bashrc

It is also possible to checkout by date. Note that in this example the file target is just a . (period). This tells git to checkout the entire tree at the given version (or the given age, in this case).

git checkout master@{1 month 2 weeks 3 days 1 hour 1 second ago} .

Show how local refs map to remote

git for-each-ref --format='%(refname:short): %(upstream:short)' refs/heads

Can also be done for a single local branch using config:

git config branch.${BRANCH_LOCAL}.remote


delete local and remote branches

Delete a local branch.

git branch -d branch_name

Delete a remote branch. Note the cryptic syntax with the :. Remember, this is dangerous.

git push origin :branch_name

git pruning remote branches

If a remote branch has been pulled into your local repository and then subsequently removed from the remote then your local copy of the branch will remain until you prune it. The git remote prune command can be used to clean up stale branches. Use the --dry-run option to list the stale branches that would be deleted.

git remote prune origin --dry-run
# then
git remote prune origin

git push a bundle

Sometimes you cannot directly push to a remote and the remote cannot pull from you. This can also happen if you clone from a repository that has a branch checked out (the remote is non-bare). The solution is to create a bundle. This is what would have been pushed to the remote. You can then transfer this bundle to the remote somehow (email, scp, etc.) and have the remote pull the bundle.

The main concern is specifying the range of commits that you want to bundle because git has no way of knowing how far back in time it was when your copy and the remote were last identical. If you don't specify how far back in history to bundle then the default is to bundle the entire history. This is equivalent to making an archive of the entire repository. Luckily, the bundle command allows you to specify a range of history by number of commits or by time.

This would bundle the last commit:

git bundle create noahs_changes.bundle --max-count=1

This bundles the last commit:

git bundle create noahs_changes.bundle HEAD^..

This bundle all commits since a given commit ID. Note that this would be every commit after the given commit ID. If you want to include the given commit ID you need to add a ^.

git bundle create noahs_changes.bundle 531a17dacdf23b9c0ebfd6b0fd60b7c41b8798ab^..

Now email or scp this bundle file to the remote. On the remote side do something like the following with the bundle. Note that FETCH_HEAD is the special location where git stores fetched objects. Notice that git log is used to examine the contents of FETCH_HEAD before it is merged.

git ls-remote .
git ls-remote noahs_changes.bundle
git fetch noahs_changes.bundle
git log -p ..FETCH_HEAD
git merge FETCH_HEAD

Or, if you trust the bundle simply do this:

git pull noahs_changes.bundle

After merging you should see that a git log on the local and remote repositories should give identical results. Note especially that the commit IDs match on both sides.

Note that git format-patch can be used for similar purposes, but it does not preserve history, so if the remote were to apply your patch you would end up with identical contents of files in the repository but with different commit IDs and history.

detached

A deteched HEAD state happens when your local working copy is not associated with any branch. This means you are looking at code that you cannot commit to any branch. This happens when you checkout a tag, or during some situations where you have a merge conflict to resolve. You can see this condition by running the following:

git checkout master^0
# or
git checkout HEAD^0

To get back to normal simply checkout the branch you were previously working on:

git checkout master

show untracked files

git ls-files --other --exclude-standard

show staged files (show added files, show cached files)

The term cached is less used now. It seems that staged is preferred. For example, compare the operation of git ls-files --cache to git diff --cached --name-only.

Git terminology can be confusing. There are synonyms for many terms. When you add a file you are adding it to the cache, which is also called stage. Some git commands even have synonyms. For example, the following two commands are identical in function. This lists the names of files that have been staged. In other words, this lists the names of files that have been added to the cache. Remember, this is what you do just prior to a commit ('git add hello_world.c).

git diff --cached --name-only
git diff --staged --name-only

I wish there was a more plumbing way to do this. This example uses the porcelain interface.

How to use git diff

git diff
shows diff between the working directory and the index. This show your working copy changes that have not been added to the index.
git diff --cached
shows diff between index and the HEAD. The HEAD is the last commit. This show what would be added by the next commit.
git diff HEAD
shows a diff between the working directory and the HEAD (the last commit).
git diff HEAD~1
If you already did a commit, this will show you what that commit changed.
git diff origin
(or git diff origin/HEAD) shows a diff between what is committed locally and the remote origin. Note that you must run git fetch or git remote update to fetch the index of the remotes. Otherwise you will only see the diff since the last time you ran a command to download the remote index. Don't use git pull in this situation because, while it will download the remote index, it will also merge and commit the diffs to your index, so then there would be no diffs to see. You can get similar information using git rev-list origin..HEAD, which will show you rev IDs (see #show ahead of origin).
git diff HEAD^ some_file.txt
shows diff of a file with a previous version.
git diff HEAD^^ some_file.txt
shows diff of a file with two versions in the past.
git diff HEAD@{3} some_file.txt
shows diff of a file three versions in the past.

If you just want to see the names of the files with diffs instead of the delta itself then add the --name-only option to git diff.

git diff --name-only origin

show the current branch and remote origin

This shows your branch while gracefully handling the case when your local is detached. This doesn't tell you anything about the origin. If you are detached then you are not tracking any remote. For testing this you can use git checkout HEAD^0 to put your working directory in a detached state. The symbolic-ref command shows which branch the given symbolic ref refers to. The HEAD symbolic ref is the working directory, so this shows which branch that the working directory is on.

( git symbolic-ref HEAD 2>/dev/null || git rev-parse HEAD 2>/dev/null ) | sed "s/refs\/heads\///"

Here are some other ways to get the branch:

git describe --contains --all HEAD
git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/\* \(.*\)/\1/'

This shows both the remote and branch you are on.

git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD)

Another way to do this is to use a combination of two commands.

REMOTE=$(git remote)
REMOTE_BRANCH="${REMOTE}/$(git rev-parse --abbrev-ref HEAD)"

Also note that if your working code is detached then the following will show HEAD instead of a remote branch:

$(git rev-parse --abbrev-ref HEAD)

Putting this all together in a script snippet:

REMOTE_BRANCH=$(git rev-parse --abbrev-ref HEAD)
BRANCH=$(git symbolic-ref HEAD 2>/dev/null | sed "s/refs\/heads\///")
if [ -z "${BRANCH}" ]; then
    BRANCH="Detached: $(git rev-parse HEAD 2>/dev/null)"
fi

Git is simple, no?

show ahead of origin

See #show the current branch and remote origin for finding REMOTE_BRANCH.

git rev-list ${REMOTE_BRANCH}..HEAD

That will list each commit ID you are ahead of the remote branch. Pipe through wc to get a count

git rev-list ${REMOTE_BRANCH}..HEAD | wc

show contents of a file at a given revision

git show ${COMMIT_ID}:${FILE_PATH}

Use git log to find a commit ID that you are interested in viewing. The use git show with the commit ID, a colon, and the filename. You may truncate the commit ID as desired.

git log ./maze.py
git show a25bc6d73d55529d26294cea96b5d740302f2130:./maze.py
git show a25bc6d7:./maze.py

If you only ask git to show the commit ID then it will display the diff of the entire commit, which may include other files.

git show a25bc6d73d55529d26294cea96b5d740302f2130

List the file names in a given commit ID

This lists the files in commit ID a25bc6d73d55529d26294cea96b5d740302f2130.

git diff-tree --no-commit-id --name-only -r a25bc6d

See what would change before a 'git pull'

The git pull command is a combination of a fetch and a merge. It is best to avoid git pull because it hides important details. Instead use git fetch, then see what changes have been made before you run a git merge into your local branch. Fetch can never mess up your local copy since it simply makes a local mirror of what is on the remote. After doing a git fetch you can use any of the following commands to examine the remote changes.

git fetch
git log HEAD..origin
git log -p HEAD..origin # shows each patch
git diff HEAD...origin # with three dots shows a single diff of all changes.
git cherry-pick

error: failed to push some refs

   33c7f02..d82b08f  dev -> dev
 ! [rejected]        release -> release (non-fast-forward)
error: failed to push some refs to 'username@git.example.com:engineering.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

The important thing to notice is that the dev branch succeeded, but the release branch was rejected. This can be confusing if you have the dev branch checked out. If you didn't read the message carefully you might think that your dev push was rejected. You might then do all the things you are told to do to fix the problem (git pull) on the dev branch. This, of course, will have no effect. The solution is to switch to the release branch and bring your copy up to date before pushing again.

git checkout release
git pull
git push
git checkout dev

What makes this confusing is that git pull seems to update the branch you have checked out; whereas git push will push all the branches you have edited.

git checkout

The following creates a branch, checks it out, and tracks it to a remote origin all in one step.

git checkout -b <BRANCH_NAME> origin/<BRANCH_NAME>

create from tag

git checkout <TAG_NAME) -b <NEW_BRANCH_NAME>
git push origin <NEW_BRANCH_NAME>

Push only a single branch to a remote

By default, git will push all of your local branches to remote branches. You can push only the checked-out branch with the following:

git push HEAD
# older style
git push origin release

You can set this behavior as the default with the following configuration:

git config push.default simple
# older style
git config push.default upstream

git remote

When using git-remote over ssh it seems that you have to use the full path on the remote server, and not the path relative to the user you are connecting as. Also, you must put a trailing slash on the path. For example:

git ls-remote ssh://username@git.example.org/home/username/repositorium/engineering/

find which branch has a commit ID

Sometimes I have a commit ID, but I don't know what branch it's in. The following will show which branch contains the given commit ID.

git branch --contains <HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH>

find available branches on remote

This will show which branches are available on a remote server. It will also show the latest commit ID for each branch.

git ls-remote origin

convert clean repository to 'bare'

Bare repositories on a remote can take two forms. Both of these will behave the same if you clone with git clone user@example.com:repo.

~/repo/.git/
~/repo.git/
cd repo
git config --bool core.bare true

or

mv repo/.git repo.git
git --git-dir=repo.git config core.bare true
rm -rf repo

Or do this:

git reset --hard HEAD

When pushing to a remote repository that you have cloned from you might get an error like this:

remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.

Converting the remote repository to a bare repository will fix this.

git clone

git clone
git fetch
git pull

git merge

Checkout the branch that you want to merge into. In this example, I want to merge a branch named feature-pxe-boot back into master.

git checkout master

Then just merge feature-pxe-boot

git merge feature-pxe-boot

The merged files are automatically committed. All you have to do is push if you want other people to see the merged commits.

git push

git log

The default format for git log does not show the files that were modified in a commit. The following adds the list of modified files to the log output.

git log --name-status

See also #config for the alias git ll which does the same thing.

git show

git show <object>

Example, after finding the refid of a commit using git log you can show what changes were actually part of that commit.

$ git show 263fe4ca6744757f596f12750cd4a8b1412d69e8
commit 263fe4ca6744757f596f12750cd4a8b1412d69e8
Author: Noah Spurrier <noah@noah.org>
Date:   Tue Feb 19 12:19:41 2013 -0800

    Documented this feature because I forgot where I put it.

diff --git a/XenResources/config/xm4.tmpl b/XenResources/config/xm4.tmpl
index 8e949d3..d476222 100644
--- a/XenResources/config/xm4.tmpl
+++ b/XenResources/config/xm4.tmpl
@@ -17,6 +17,9 @@
   }
 }
 memory      = '{$memory}'
+# This allows you to get the name of the Xen host while inside the guest.
+# Do something like this to get the vmhost name:
+#     VMHOST=$(sed -e 's/^.*vmhost=\([^[:space:]]*\)$/\1/' /proc/cmdline)
 extra = "vmhost="+os.popen('hostname -f').read().strip()
 
 #

config

The git config command will update ~/.gitconfig.

git config --global user.name "Noah Spurrier"
git config --global user.email noah@noah.org
git config --global core.editor vim
git config --global color.ui auto
git config --global merge.tool vimdiff
git config --global push.default simple

You can also edit the ~/.gitconfig file directly. The following also contains a few simple aliases that I like to use.

[user]
    name = Noah Spurrier
    email = noah@noah.org
[core]
    editor = vim
[color]
    ui = auto
[merge]
    tool = vimdiff
[push]
    default = simple
[alias]
    # See also https://git.wiki.kernel.org/index.php/Aliases
    retrack = "!retrack() { git config \"branch.$1.remote\" $(dirname \"$2\"); git config \"branch.$1.merge\" \"refs/heads/$(basename \"$2\")\"; }; retrack"
####graph = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
    graph = log --graph --pretty='format:%h,%ci,%d, %s <%ae>'
    ll = log --name-status

git archive

This is like an export. It retrieves a remote git repository without the .git metadata (history).

git archive --remote=ssh://username@www.example.org/home/username/repositorium/engineering/ HEAD > git_archive.tar

You can also use this to retrieve subdirectories of repositories. The follow retrieves only the subdirectory "dotfiles". Note that the selection of subdirectory is made after HEAD::

git archive --remote=ssh://username@www.example.org/home/username/repositorium/engineering/ --prefix=dotfiles/ HEAD:dotfiles > dotfiles_archive.tar

fix commits with the wrong committer/author name or email address

This basically completely rewrites your history. All of your git ref hashes will change.

git filter-branch --env-filter 'GIT_AUTHOR_NAME="Noah Spurrier";GIT_AUTHOR_EMAIL="noah@noah.org";GIT_COMMITTER_NAME="Noah Spurrier";GIT_COMMITTER_EMAIL="noah@noah.org";' HEAD

Git Commit: git/svn commit differences are confusing to newbies

Do a `git commit -a` in most cases.

Git stages commits into an index before they are committed, so a commit is a two step process. The `git-status` command will show which modifications have been indexed for the next commit. The `git-add` command will index a modified or new file for the next commit.

A `git fetch` also is done as a two step process. I especially like this feature. When you do a `git fetch` files are pulled from a remote server, but they are not immediately merged into your working copy. I despise this about SVN. Instead the remote files (actually their diffs) are held in the index where you can browse them and choose which parts to merge into your working copy.

Notice that the message below from `git-status` says two things. It shows files that have been modified and it shows which of those changes have been added to the commit. Notice here that one file is modified, but no changes have actually been added to the commit.

$ git-status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   dotfiles/.vimrc
#
no changes added to commit (use "git add" and/or "git commit -a")

You can have git automatically index all modified files with `git commit -a`. This is usually what most SVN users expect.

git-svn

dcommit

  1. When you want to do "svn up", instead do "git svn rebase". If you have conflicts use `git mergetool` then `git rebase --continue`.
  2. When you have everything up-to-date do `git svn dcommit`.
  3. If git complains about files not being up-to-date do `git svn rebase` again.

VCS command translation

See also this table in Wikipedia: VCS commands

Rosetta table gives git and Subversion equivalent commands
git subversion notes
git clone url svn checkout url `git+ssh` works just like `svn+ssh`
git clone git+ssh://noah@www.example.org/home/noah/src
git pull svn update
git status svn status The output from git is more clear.
git -f checkout path svn revert path This deletes your local changes.
git add file svn add file
git rm file svn rm file
git mv file svn mv file
git commit -a svn commit
git log svn log
git show rev:path/to/file svn cat url
git show rev:path/to/directory svn list url
git show rev svn log -rrev url
git branch branch_name svn copy http://example.com/svn/trunk http://example.com/svn/branches/branch_name
git merge --no-commit branch_name svn merge -r 20:HEAD http://example.com/svn/branches/branch_name This is reason #1 why SVN sucks. Merge tracking is crap. As of version 1.5 they have some limited support for merge tracking, but it still blows.
git tag -a tag_name svn copy http://example.com/svn/trunk http://example.com/svn/tags/tag_name SVN doesn't really have tags. Most people just use copy. The downside is that people can commit to tags, which kind of defeats the purpose.

Cheat Sheet

Originally from http://cheat.errtheblog.com/s/git

Setup


git clone <repo>

 clone the repository specified by <repo>; this is similar to "checkout" in
 some other version control systems such as Subversion and CVS

Add colors to your ~/.gitconfig file:

  [color]
    ui = auto
  [color "branch"]
    current = yellow reverse
    local = yellow
    remote = green
  [color "diff"]
    meta = yellow bold
    frag = magenta bold
    old = red bold
    new = green bold
  [color "status"]
    added = yellow
    changed = green
    untracked = cyan

Highlight whitespace in diffs

  [color]
    ui = true
  [color "diff"]
    whitespace = red reverse
  [core]
    whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol

Add aliases to your ~/.gitconfig file:

</pre>

 [alias]
   st = status
   ci = commit
   br = branch
   co = checkout
   df = diff
   lg = log -p
   lol = log --graph --decorate --pretty=oneline --abbrev-commit
   lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
   ls = ls-files

</pre>

Configuration


git config -e [--global]

 edit the .git/config [or ~/.gitconfig] file in your $EDITOR

git config --global user.name 'John Doe' git config --global user.email johndoe@example.com

 sets your name and email for commit messages

git config branch.autosetupmerge true

 tells git-branch and git-checkout to setup new branches so that git-pull(1)
 will appropriately merge from that remote branch.  Recommended.  Without this,
 you will have to add --track to your branch command or manually merge remote
 tracking branches with "fetch" and then "merge".

git config core.autocrlf true

 This setting tells git to convert the newlines to the system’s standard
 when checking out files, and to LF newlines when committing in

You can add "--global" after "git config" to any of these commands to make it apply to all git repos (writes to ~/.gitconfig).


Info


git reflog

 Use this to recover from *major* fuck ups! It's basically a log of the
 last few actions and you might have luck and find old commits that
 have been lost by doing a complex merge.

git diff

 show a diff of the changes made since your last commit
 to diff one file: "git diff -- <filename>"
 to show a diff between staging area and HEAD: `git diff --cached`

git status

 show files added to the staging area, files with changes, and untracked files

git log

 show recent commits, most recent on top. Useful options:
 --color       with color
 --graph       with an ASCII-art commit graph on the left
 --decorate    with branch and tag names on appropriate commits
 --stat        with stats (files changed, insertions, and deletions)
 -p            with full diffs
 --author=foo  only by a certain author
 --after="MMM DD YYYY" ex. ("Jun 20 2008") only commits after a certain date
 --before="MMM DD YYYY" only commits that occur before a certain date
 --merge       only the commits involved in the current merge conflicts

git log Cite error: Closing </ref> missing for <ref> tag