Saturday, February 18, 2012

Version Control with Git

Updates: 11/21/2012 

I'm now using Git with a Subversion repository at work

About all I do to interact with Subversion are git svn rebase and git svn dcommit.  The first command I use to update my local master and the second to commit to the remote Subversion repository.  We create branches for just anything but the most minor change, rebase to the local master and commit to the Subversion repo from there.


git stash

My favorite find (and one which I didn't see mentioned in my edition of Pragmatic Version Control Using Git) is git stash.  git stash lets you throw changes in a locker and brings your current branch back to the last checkout state.  I use this all of the time, but one frequent use is to quickly test whether and/or which of my local changes is causing a test to fail or online functionality to get weird.  git stash list shows all of the stashes that you have, uh, stashed.  git stash apply brings changes back from the stash.

Git UI tools

At my new shop developers are all on Macs.  Some folks use GitX and others use SourceTree, but I've found the command line is quick and easy enough.  And I prefer the Eclipse file diff functionality to the diff mechanisms of either of the aforementioned GUIs.  Since Eclipse isn't in the loop with my local Git repo, I just compare with local history if I have  a large enough change I need to diff.



From 02/18/2012

I've worked quite extensively with CVS and Subversion.  With the latter I've actually set up and administered my own repositories.  But I never read much about either VCS outside of picking through http://svnbook.red-bean.com/ when I couldn't figure out how to do something.


I've been using Git on an off for about six months for a variety of non-work projects and figured it was time to understand what I was doing so that I could properly maintain branches and tags and conduct merges.  Enter Jon Loeliger's Version Control with Git for a good (albeit somewhat old -- 2009) overview.

Turning  local repo into an authoritative remote repo
I had a local repo that I wanted to share with my labmates.  The process involved cloning my local repo to another location

$ git clone --bare ./local_repo remote_repo.git
$ zip -r remote_repo.git.zip ./*
$ mv remote_repo.git.zip /gitpub/Depot
$ unzip remote_repo.git.zip

and then modifying my local repo to point to the new remote one

$ git remote add origin "//192.168.1.1/gitpub/Depot/remote_repo.git"
$ git remote update
$ git branch -a
  * master
  remotes/origin/master


Eclipse integration
Plugins
EGit seems to work pretty well.  I'm having some issues with pulls and fetches, from my repo that used to be the authoritative one, but the command line gets me what I need.  Push and synch are fine.

Diff local with remote repository

  • Right click on the project or a specific directory/file
  • Compare With -> Branch, Tag or Reference... -> Remote Tracking -> origin/master


User stuff
Indexes
I can modify a file, run git add and then continue to modify the file.  The index will not be updated with my local version of the file until I run git add again.

What I'm not used to from other VCSs is the fact that, from the command line, I can add a file (place changes in the index) and have  a different version of the file in the local repository.  With Subversion or CVS I just think of the repository version and my working version -- there's not a 'staged' version in any index.  When I execute a commit, only the changes that are in the index are committed -- the local directory file is not committed.

Diff local with remote repository

From the command line, git diff origin/master --name-status will give you the names of the different files and not clutter up the console with the differences themselves.


Fetch versus pull
I saw a pull described as "a fetch followed by a merge".


Admin stuff
Examine object (blob, tree, etc.) content.  This output could be the contents of a file or a directory.
git cat-file -p <dir_prefix><rest_of_SHA1_for_blob>


List files/directories in an object
git ls-files -s


Other resources
Pro Git

No comments:

Post a Comment