Back to posts.

Git Snippets

Save changes in new branch

When you made some changes and want to store them in a new branch, and you did not yet commit your changes, you can execute the following. Make sure that you did not commit the changes yet.

$ git branch -b your_new_branch
$ git commit -am "Changes in new branch!"
$ git push origin your_new_branch

Make a local copy of a remote branch

When you have a new branch and you do a clone on another location you need to checkout the remote branch first, and make a local version of it.

$ git remote update
  * [new branch]      feature_heightfield_fix -> origin/feature_heightfield_fix
$ git checkout -b feature_heightfield_fix origin/feature_heightfield_fix

Push new branch to remote

Here roxlu is the new branch that we would like to push to origin.

$ git push -u origin roxlu

Clone a repository withouth all history

git clone --depth 1 --branch master git:// .

Create a bare repository

You can use a bare repository to allow other users to push changes to your own server. To create a bare repository you use:

$ login git
$ cd /Users/git/repositories
$ mkdir roxlu.git
$ cd roxlu.git         
$ git init --bare .

Then you can checkout this repository by something like:

$ cd Documents/programming/roxlu
$ git clone git@localhost:/repositories/roxlu.git .

... or you can push an existing repository to the newly created bare git

$ cd existing/repository
$ git remote add roxlu git@localhost:/repositories/roxlu.git 
# git push --all roxlu