Git

From Attie's Wiki
Revision as of 01:00, 8 December 2011 by Attie (Talk | contribs)

Jump to: navigation, search

My git cheat sheet! See my svn cheat sheet

This might also be useful: http://git.or.cz/course/svn.html#branch

Contents

Commands

command description
git clone <path> retrieves the entire git repository to your local disk
git pull pulls any new revisions from the original path
git push pushes local revisions to the original path
git commit commits to the LOCAL repository
git status status since last commit
git tag lists the avaliable tags
git checkout <branch/tag> changes the working copy to the tag specified
git reset --hard reverts all changes, to the currently checked out tag
git reset --soft HEAD^ reverts the HEAD pointer to the previous, but KEEPS your changes and index
git reflog shows a simple log with 7 characters of the has, refspec and commit message
git fetch <remote-repo> <remote-branch>:<local-branch> fetches the remote branch
git push origin master:master first-time push
git push <remote-repo> <remote-branch>:<local-branch> push the branch
git remote add <local-identifier> <remote-address> add a remote server that you are able to push to
git branch -a list all branches remote and local

.gitconfig

[user]
  name = "Attie Grande"
  email = "attie@attie.co.uk"
 
[alias]
  st = status
  ci = commit -v
 
[color]
  ui = true

First push into a fresh repository

git add x y z
git ci -m "first"
git push origin master

Undo last commit

git commit ...
git reset --soft HEAD^      # 1
# edit files                # 2
git add ....                # 3
git commit -c ORIG_HEAD     # 4
  1. This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before "reset".
  2. Make corrections to working tree files.
  3. Stage changes for commit.
  4. "reset" copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead.

Import SVN history into a new GIT repoistory

Simples! The 'users.txt' file looks like this:

attie = Attie Grande <attie@attie.co.uk>
git svn init http://subversion/repo --no-metadata ./repo
cd repo
git config svn.authorsfile ../users.txt
git svn fetch
 
git remote add origin /path/to/new/git/repo
git push origin master:master

Getting the first tag that contains a file

Tig?

#!/usr/local/bin/bash
if [ "$1" == "" ]; then
        echo "usage: $0 <filename>"
        exit 1
fi
 
if [ ! -e $1 ]; then
        echo "$0: File does not exist ($1)"
        exit 1
fi
 
echo -en "Finding first commit..."
COMMIT=`git log --pretty=oneline "$1" | tail -n 1 | cut -d " " -f 1`
if [ "$COMMIT" == "" ]; then
        echo "$0: Error while retrieving commits for file \"$1\""
        exit 1
fi
 
echo -en "\rFinding tags containing commit $COMMIT..."
git tag -l --contains "$COMMIT" > /tmp/tag_contains
if [ "$?" != "0" ]; then
        echo "$0: Error while retrieving tags containing commit \"$COMMIT\""
        exit 1
fi
 
echo -en "\rFinding first tag that contains $COMMIT..."
git for-each-ref --sort='-*authordate' --format='%(tag)' refs/tags/ |
while read a; do
        if [ "$a" == "" ]; then
                continue
        fi
        grep "$a" /tmp/tag_contains > /dev/null
        if [ "$?" == "0" ]; then
                LAST=$a
        else
                echo "$LAST" > /tmp/tag_first
                break
        fi
done
TAG=`cat /tmp/tag_first`
rm /tmp/tag_contains /tmp/tag_first
 
echo -en "\rFinding tag information..."
INFO=`git show "$TAG" | head -n 3 | tail -n 2`
if [ "$INFO" == "" ]; then
        echo "$0: Error retrieving info for tag \"TAG\" (maybe it isn't annotated?)"
        exit 1
fi
 
echo -en "\r\0033[K"
echo -e "File:\t\t$1"
echo -e "First tag:\t$TAG"
echo "$INFO" | sed -re 's/^(.+):[ ]+(.+)$/\1:\t\t\2/'
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox