this page contains frequently used git commands when working on git projects with more than one developer. it contains:

  • global git configuration
  • creating a new git repository
  • adding a remote to the either existing or newly created git repo
  • merging commits when working together with several developers

Global setup:

 Download and install Git
 git config --global user.name "Your Name"
 git config --global user.email js@lastlog.de
 Add your public key

credentials cache:

 git config credential.helper cache
 https://git-scm.com/docs/git-credential-cache

nice settings

  [color]
       ui = auto
  [push]
       default = current

note: see http://stackoverflow.com/questions/11872984/what-is-the-difference-between-git-push-default-current-and-push-default-upstrea

it is also helpful to have either:

* zsh with grml's zshrc (this is what i use) [3]
* Liquid prompt for bash/zsh (which i find interesting) [4]

Next steps:

 mkdir nNull
 cd nNull
 git init
 touch README
 git add README
 git commit -m 'first commit'
 git remote add origin git@github.com:qknight/nNull.git
 git push origin master

Existing Git Repo?

 cd existing_git_repo
 git remote add origin git@github.com:qknight/nNull.git
 git push origin master

MELD

   [merge]
           # http://stackoverflow.com/questions/11133290/git-merging-using-meld   
           tool = mymeld
           conflictstyle = diff3
   [diff]
           tool = mymeld
           conflictstyle = diff3
   [mergetool "mymeld"]
           keepBackup=false
           #trustExitCode=true
           # When there's a collison during git merge, I open a mergetool called Meld. It opens three files LOCAL, BASE and REMOTE. 
           #As I've read 
           # - LOCAL -> what is up2date at master
           # - BASE -> stuff i put together from left and right
           # - REMOTE -> what i did
           cmd = /run/current-system/sw/bin/meld --diff $BASE $LOCAL --diff $BASE $REMOTE --diff $LOCAL $MERGED $REMOTE


frequently used git commands

adding a new master

 git remote add crei http://git.gitorious.org/evopedia/evopedia.git

my .git/config

 [core]
       repositoryformatversion = 0
       filemode = true
       bare = false
       logallrefupdates = true
 [branch "master"]
 [remote "origin"]
       url = git@github.com:qknight/evopedia.git
       fetch = +refs/heads/*:refs/remotes/origin/*
 [remote "crei"]
       url = http://git.gitorious.org/evopedia/evopedia.git
       fetch = +refs/heads/*:refs/remotes/crei/*

fetch & merge

let's merge my current head with crei/master using a fetch & merge operation. as mine src.pri is outdated i decide to use his src.pri by overwriting it with a checkout.

 git fetch crei master
 git merge crei/master
 git checkout --theirs src.pri
 git add src.pri
 git commit

push my stuff to github.com

afterwards i commit his changes and then i push it to my repository at github.com

 git add <several files>
 git commit
 git push origin master

upload changes done in windows

problem: had to work with git on windows but i did not want to push then changes from windows. instead i did use it the normal way: git add <stuff>; git commit;

finally i used zip to make an archive of the whole direcotory (where the git stuff was in). next i extracted it on linux but this resulted in crazy things as well files were different (encoding problems). cloning it locally fixed it. finally i reseted the head and recommited everything from scratch (but this time as my main user).

in retrospective i could have used the local git socked based service to clone it from windows directly.

i had to use this method because all the files had a different encoding and i couldn't use the changed files directly. see 'recommit' at [2].

on windows

 tar cf evopedia.tar evopedia/

now on the linux machine:

 cp evopedia.tar /tmp
 tar xf evopedia.tar
 mkdir /tmp/evopedia-windows-clone/
 cd /tmp/evopedia-windows-clone/
 git clone /tmp/evopedia/

check that your wanted git commit is there

 git log

next copy the git configuration from the place the project is in usually

 cp ~/Desktop/projects/eopedia/.git/config .git/

now reset the last commit (one can directly recommit afterwards)

 git reset --soft HEAD^

to check if everything is sane

 git status 
 git commit

now upload the stuff to the remote: origin master

 git push origin master

got regression?

in case you get regression, make a commit with the current work

 git status 
 git add .... various files
 git commit

next we want to see a list of previous commits

 git log

which will print out all your commits with a SHA1 sum, find the commit's SHA1 sum to branch into your previous commit

 git checkout -b aNewBranch e0184ebe889cb4f4fb70a50f1523a77b2b6070a9 

now you can play with the 'previous commit' as all files in the current dir are 'timetraveled backwards'

as i usually work with cmake and out of source build it's simply

 rm -Rf build/*
 cd build
 cmake ..
 make

after the software has built and you found out that it really worked in the past but does not work in head it's a clear sign of regression (which happens often when not using class tests).

therefore i started to maintain a test script, to monitor parameters as:

  • are certain files installed
  • are binaries built actually executable? i grep for '--help' to see that they do

finally it's time to go head, do this:

 git checkout master

and you are back, you can now remove the branch (or as you desire leave it for further experiments).

Your branch is ahead of 'origin/master' by 4 commits

run:

 git status

to get this message

it happens when you pulled from another master which has several commits, which might already be merged with your local repository. but you need to upload the local repository to _your_ online repository (for instance github.com).

to see what is going on:

 git diff origin/master 

in order to upload my local repo to my online repo, i type:

 git push origin master

rebase

a few often used rebase commands:

   git branch -b my-feature-branch
   git add myfile
   git commit -m 'new commit'
   git checkout master
   git pull --rebase
   git checkout my-feature-branch
   git rebase master
   git checkout master
   git merge my-feature-branch
   git push origin master (or)
   git push upstream master 

git over ssh

http://www.mindfuzz.net/?p=250

multiple workdirs

makes it easy to work on several different branches, like nixpkgs, from different directories

goodies

tic (git with ncurses) git rev-parse (and the manpage)

links

Powered by MediaWiki