branch

git: Useful commands

Cloning a branch

This is mostly a notes for me… for cloning a branch directly putting it in directory of your choosing.

Here is the command :

git clone -b my-branch ssh:/.....  my-dir

you don’t have to clone, cd, then change branch …etc… and you have it in directory of your choosing.

Revert changes

This is how to revert all changes you made after the last commit. So if you think your latest changes are no good you can rollback to the last stable state, like in the databases.

git reset HEAD --hard

Git: creating clean branch

Sometimes you need to create a clean branch in git that has no history.
This is the case for example when you need to have two or more code bases, but you are have just a single repository. Sad but happens !
You have to do with what you have.

In this case use the following recipe :

#fetch the original repo.
#Normally you would like dir_name to be the same as the new branch name
git clone ssh://.... dir_name
cd dir_name
#create the orphan branch
git checkout --orphan blah
#cleanup all the files, you dont need them, right ? 
git rm -rf .
#add a file so you can commit
echo start > readme.txt
git commit -m 'creating an orphan branch ...' -a
#now the branch shows up
git branch
# ... no commit log, like we wanted
git log
# time to make the branch show up in the remote repo
git push -u origin blah