• Workflow for pull/rebasing branches using gerrit for codereview:
    REMOTE                 LOCAL

    master  r1             master  r1

    (working and commiting in private branch r1)     
     |      |               |      |
     |   git pull origin master    |     
     +----------------------------->
     |      |               |      |
     |      | git push -f origin r1
     |      <----------------------+
     |      |               |      |

    (pushing r1's changes to master)
     |       git pull origin r1    |
     |      +--------------->      |
     |      |               |      |
     |   git push origin master:refs/for/master
     <----------------------+      |
     |      |               |      |

This assumes you configured pull to do a rebase, meaning you have in ~/.gitconfig:

    [branch]
	autosetuprebase = always

And in your clone's .git/config:

    [branch "XXXX"]
	remote = origin
	merge = refs/heads/XXXX
	rebase = true

for every branch.

  • Edit the last commit message, prior to pushing:
    $ git commit --amend
  • Creating a local branch and pushing/pulling it to the master repo:
    $ git branch erwan/clientserver
    $ git branch
      erwan/clientserversplit
    * master

    # push the new branch to the remote repo
    $ git push origin erwan/clientserver

    # switch to the new branch
    $ git checkout erwan/clientserver
  • Importing a remote branch:
    # find out the remote branch:
    $ git branch -r
    origin/HEAD -> origin/master
    origin/master
    origin/stable
    origin/testing

    # import stable and testing under the same local names
    $ git checkout --track -b stable origin/stable
    $ git checkout --track -b testing origin/testing
  • Push/pull changes in local branch to remote repo:
    $ git push origin erwan/clientserver

    $ git pull origin erwan/clientserver
  • Merging branches:
    # move to the destination branch
    $ git checkout testing

    # diff between testing and master
    $ git diff master..testing

    # merge testing with master
    $ git merge master

    # push to remote
    $ git push
  • Removing a remote branch:
    # find out the name of the remote repo:
    $ git remote show
    origin

    # show remote branches
    $ git branch -r

    # delete the branch 'bla/bleh' on the remote 'origin'
    $ git push origin :bla/bleh

    # then delete the branch locally
    $ git branch -d bla/bleh
  • Discarding the latest (local) commit:
    $ git reset --soft HEAD^
    $ git status
    # shows your local branch is 1 commit ahead of master

    $ git diff master
    # check that the diff is what you wanted to discard

    # then discard it:
    $ git reset HEAD <file>
    $ git checkout <file>
  • Useful git log variants:
    $ git log --pretty=format:"%h %an %ad %s" --date=short | tac | less

    $ GIT_PAGER='less -n +/^commit\ \[0-9a-f\]+' git log --color -p --stat

    $ git blame [filename] | awk '{ print $2 }' | sed -e "s/(//" | sort | uniq -c