Skip to content

GIT Survival guide

simboss edited this page Jul 22, 2012 · 3 revisions

Working on branches different than master

Let's say you want to work on the 2.1.x branch from remotes. By default, after you clone this repo, your local repo does not have a branch for 2.1.x therefore if you try to do something like: git checkout remotes/2.1.x you'll end up having a local repository with no name and that is in detached HEAD state.

The proper way to switch to a branch that is not master is as follows:

  1. create a local branch with a proper name, e.g. 2.1.x
  2. have the local branch track the remote one

We can achieve this with a single command

git checkout -b 2.1.x origin/2.1.x

Reverting

I just learned that if you have some changes in your working tree that you want to get rid of, you don't type 'git revert' like you might guess. No, that's what cvs, subversion, mercurial, and bazaar (to name a few) use revert to mean, but not git. With git, revert is used to undo actual commits. Thankfully, you can undo your revert with another 'git revert', I just learned. So let me repeat to be clear, if you have changes to your working files that you want to abandon, DO NOT do this:

git revert HEAD

That will undo your last commit. Do this instead:

git reset --hard HEAD