A basic GIT guide for the SVN user

old post. I needed more git info today (18.mar-2010) and found this great wiki! http://git.wiki.kernel.org/index.php/GitCheatSheet

I know there’s been written a lot about this, but either way I’ve found it so hard to switch from subversion and keep mixing stuff! I still consider myself a git newbie, not even liking git too much yet. I guess it’ll grow on me, though. Having spent too much time struggling, I’ve finally learned to master some simple techniques, as undoing changes, undeleting and removing new files not yet under source control. I’ll show these after the following desparate attempt to explain how the repository concepts differ fundamentally. This you must understand:

Subversion versus git – the repo conceptual difference

Subversion: 2 levels:

  • local checkout  – Files on your computer, in the file system ( svn checkout https://svn.xx.yy/projectxyz)
  • server, the sentral subversion repo – The server which every user checks in to (svn commit) and checks out from (svn up)

Git: 3+ levels:

  • local file system of a clone (git clone git@github.com:oma/projectxyz as checkout in svn). As above, yet different… Must be seen in context of the local index, below.
  • local index og a checkout (clone) in the same file system. This is similar to server above. Hang on, I’ll try explain. You mess around with the local files as always (level 1), then you commit to the local index (git commit -a -m “msg”).  The git repo server is still unchanged. Then make more changes and commit and so on. Any commits you make, are written to the index, stored in the directory .git, locally. Unlike subversion, which creates a .svn directory within eachdirector, git has only one .git directory. This enables you to create multiple branches in the same directory in the file system! It deletes and adds files automagically when switching. OK, make as many commits you want locally. When satisfied, push them all to the server. Read on below.
  • server, the sentral git repo (or one of the many!). When you’re satisified with you’re local changes and committed them locally (git commit) then you perform a git push which pushes the changes to the repo you cloned). To get changes from this repo/server, you perform git pull.
  • the “4th” level is the git server repo of each developer, which your server need to pull from and vica versa. I won’t delve into this in this post.

Holy s.. this is hard to explain! But then again, it took me a long time to understand and I’ve probably misused terms as server and indecies here.

Ok, what about the simple workflow I had in subversion? I don’t fully understand the git revert, reset, rebase commands! They are so dangerous that I made the strikethrough. I will not teach you how those work, in this post, but I will warn you that revert is very different from subversion. Use with care. Finally, to the useful KISS examples:

Status: what has changed since last commit/checkout?

Luckily, svn status and git status is similar.  Use it as frequently as you do with subversion :)

Undelete: I accidently deleted a file!

Subversion: svn up in that directory or any subdirectory.

Git: git checkout <path> does the trick! NB: Unlike svn up it will also do the equal to svn revert on all files in the path. “git checkout .” will restore all files (both modified and deleted) to the last commit (NB. the last commit to your local “repo”. Also called the index)

Git: git checkout app/model/my_deleted_file.rb will restore this specific file.

Undo: I don’t like my changes and want to start over!

Subversion: svn revert. Or delete files/directories and run svn up.

Git: git checkout <path> as described above.

Delete new files: I don’t want all those stupid files I created!

Subversion: Err, I don’t remember, are there any command for this? I guess you manually cleanup by deleting in the file system. Or, if you don’t worry about losing local modifications, delete directories and run svn up to restore them

Git: git clean -fd. first run git clean -nd (n for dry-run) to see what git would do if you run git clean -fd

Extras

Share your files

Subversion: svn commit -m “commit msg” <files>

Git: git push This will push from your local index, so remember to perform git commit -a -m “commit msg” <files> first.

Get shared files

Subversion: svn up

Git: git pull Or, if from local index git checkout

Help

Git help is pretty nice. Use git help or git help clean to read about the clean command.

Resources

Git Ready! What you don’t find here, you don’t wanna know! (ok, those were my words, hehe) http://www.gitready.com/

Git cheat sheet! http://cheat.errtheblog.com/s/git

About Ole Morten Amundsen

Developer, programmer, entrepreneur. Java, .Net, ruby, rails, agile, lean. Opinionated enthusiast!
This entry was posted in git and tagged . Bookmark the permalink.

2 Responses to A basic GIT guide for the SVN user

  1. Pingback: links for 2010-08-10 at adam hoyle presents suckmypixel

  2. Pingback: Developing HPX with Git: A Tutorial | The STE||AR Group

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s