Friday, August 28, 2009

git cheat sheet

From what I have seen, the best description of what Git is and how it works operationally was written by Charles Duan. Given that Charles has done such a great job, I am just going to summarize a few key commands here (also based on this tutorial).

A) Create a local git repo out of your current project directory.

$ cd ~/myproj
$ git init
$ git add .
$ git commit -m "first commit"

B) Create an empty remote git repo and push current project repo there.

$ ssh my.remote.server

> mkdir gitroot
> mkdir gitroot/myproj
> cd gitroot/myproj
> git --bare init
> exit

$ cd ~/myproj
$ git remote add origin ssh://my.remote.server/home/username/gitroot/myproj
$ git push origin master

C) Check out a remote git repo and send updates back.
$ git clone ssh://myserver.com/home/username/gitroot/myproj

$ git push
D) Create and switch among branches
$ git branch [new-head-name] [reference-to-branching-point]
For example:
$ git branch testing HEAD
You switch among branches simply by checking them out from your own repo:
$ git checkout testing
$ git push origin local-branch-name
E) Merge branches. Again, look here for a detailed description of how this works in practice.
$ git checkout master
$ git merge testing
F) Convert svn repos to git. There are other ways to do this if you want to preserve the tree of your svn repo; however, if all you want is to strip out the .svn folders:
$cd ~/mysvnproj
$ svn export . ../myproj
The problem is that only files under version control are exported. If you just want to remove svn version control from your current directory (i.e., recursively strip out the .svn folders), then use:
$find . -name ".svn" -type d -exec rm -rf {} \;

No comments: