Skip to content
Donald Buczek edited this page Nov 23, 2016 · 10 revisions

Cheatbook / HOWTO / FAQ

Create a new "feature branch" for a pull request

git checkout master
git pull                      # make sure, you are branching from the latest and greatest version
git checkout -b add-foobar

Rename a feature branch

git branch -m add-foobar add-barfoo

Push your feature branch to github.molgen.mpg.de

git checkout add-foobar       # be on you branch
git push                      # and push it

If you are doing this for the first time, git will not yet know, where to put the branch. But (as often) it has a good suggestion in the reply:

fatal: The current branch add-foobar has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin add-foobar

so in that case, do as you are told:

git push --set-upstream origin add-foobar

github rejects my push

! [rejected] add-foobar -> add-foobar (non-fast-forward)
error: failed to push some refs to 'git@github.molgen.mpg.de:mariux64/bee-files.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

You are welcome to force-push your own feature branch even if a pull request is open.

git push -f

I want to change my commits

If you are not happy with you commits, perhaps because someone suggested an improvement and you want to fix spelling errors, split one commit into two, remove a commit, and tidy up everything, the magic feature is git rebase -i

git checkout master
git pull                       # get the latest master
git checkout add-foobar        # go to your branch
git rebase -i master           # re-apply your changes on top of the current master. -i lets you edit them

Now you are in your editor where you can give orders, what to do with each commit. The template includes some help text.

During a rebase or merge, I've got confused and just want to get out and start over.

git rebase --abort or git merge --abort

Oops - I've accidentally committed on the master

git branch add-foobar       # create the feature branch. Now add-foobar and master are equal
git reset origin/master     # reset your branch "master" back
git checkout -f add-foobar  # switch to your feature branch

Why the -f here? Typically you've added or changed some files. When we git reset origin/master we only let our master branch point to another commit. We did not change the work directory and the files you've added or modified are still there. git status shows it.

Not if you checkout add-foobar which contains commits for the new and changed files, the checkout would overwrite the files in you working directory which are not already committed to the master branch. Git does not detect, that the files would be overwritten with the same contents. With -f we says, thats okay.

What is a good commit header line:

Examples:

  • foobar: Add version 1.2.3
  • foobar: Update version from 1.2.3 to 1.2.4
  • foobar: Rebuild version 1.2.3
  • foobar: Remove
  • foobar: Rewrite beefile from scratch
  • foobar: Compile with -O0
  • foobar: Include patch to prevent application crash
  • foobar: Fix crash problem

Hints:

  • Start with subsystem and a colon
  • First word after the colon uppercase
  • Say, what the commit is supposed to do in an imperative style ("Do this" like an order not "does this" like a description)
  • Short

I often get confused

Use git status , log --oneline --graph --decorate and log --oneline --graph --decorate --all consider typing git config --global alias.lol "log --oneline --graph --decorate" git config --global alias.loga "log --oneline --graph --decorate --all" once to get the aliases lol and loga in your ~/.gitconfig and save some typing