Alcides Fonseca

40.197958, -8.408312

git

git (this one is better for your eyes) is a DVCS created by Linus Torvalds. Besides the Linux Kernel, Ruby in Rails is one of the major projects migrating their source code to GitHub, the social network of opensource code.

Why should I use git?

Well I could say it was faster, or smaller than using SVN, but that’s not the problem to 90% of the developers. The real advantage I see is that you can commit offline, and branch and merge more easily than with SVN. You can even easily push your modifications to more than one repository online. And you’ve got submodules, which I should write about here in the future.

Getting git

Or if you feel confortable enough, you can compile it yourself

Global configurations

git config --global user.name "Alcides Fonseca"
git config --global user.email me@alcidesfonseca.com
echo ".DS_Store
Thumbs.db
Desktop.ini
*~
*.swp" > ~/.gitignore
git config --global core.excludesfile ~/.gitignore
git config --global rerere.enabled 1
git config --global rerere.autoupdate 1
git config --global push.default matching
git config --global apply.ignorewhitespace change
git config --global color.ui true
 

My workflow

Git follows the UNIX philosophy: it gives you little tools which you can use together to make your own workflow. So I usually have one central repository, another copy in my machine, and another copy in the production environment. So I first create the central one: if it’s opensource, I use GitHub otherwise in a ssh-accessible server:

mkdir example.git
cd example.git
git --bare init

Then, in the client side, I clone the repository1 to my machine (as you would checkout a SVN repository). All of the following can be done using git-gui, but I find it a bit ugly, so I stick to the command line and Textmate’s ProjectPlus.

git clone user@server:~user/projects/example.git
cd example.git

#make changes

git add . # this adds to the commit list all the files modified
git commit -m “Commit message”

  1. at this point, no code is uploaded back to the server

git push origin master # now we send the changes to the master branch of the origin server

Note: If you have more than the master branch, and you want your other branches to be on the server too, you have to manually upload it first with git push origin coolbranch and after that, all git push will upload that branches.

As you can see, you can commit locally, even if you’re offline. That’s one of the major advantages git has compared to centralized VCS like SVN.

Other useful commands:

git reset --hard HEAD^ # undo commits
git status # gives you a list of changes to commit, or not added
git mv file new_file # move
git rm file # deletes file

Branching

Another of git’s advantage against SVN is that it makes branching easier. So if you’re adding a new feature, you might want to do it in another branch.

git checkout -b newfeature 
# creates and moves to a new branch
#make changes, add and commit
git checkout master #go back to main branch
# correct a bug or something, git add and commit
git checkout newfeature # go back to the new feature branch
# make more changes
git checkout master # go back to master
git merge newfeature # and merge the two branches
git branch -D newfeature # deletes the branch

Remote Branches

Branches are local! If you want to replicate your branch in a remote repo, git push -u origin newfeature. And all pushes after that one include the newfeature branch. If you want to delete it. git push origin :newfeature.

If you want to continue working on a remote branch, you need to track it by doing git checkout -t origin/remotefeature

git-svn

You might not have a choice about the VCS you use, or for some other reasons (like external integration) you have to use SVN. Don’t worry, there’s a way of using locally git and committing to your SVN repository.

git svn clone https://svn.server,com/project -s --prefix=svn/
#or if you don't use the trunk,branches and tags:
git svn clone https://svn.server.com/project
git checkout -b work trunk # move to a second branch for your work
#make you changes, git add them and commit.
git svn dcommit # makes one SVN commit, for each of the commits you did before.
git svn fetch # equivalent to SVN update

More information about git-svn in Tsuna’s blog

Troubleshooting

Non-ASCII Windows username

If you have a Windows username with Non-ASCII characters, like “João Rodrigues”, git will not work properly because Windows doesn’t user UTF-8 for command line.

Solution:

Edit C:/Program Files/Git/etc/profile in your editor

Change: HOME="$USERPROFILE"
to: HOME="C:\Program Files\Git\home"

And that’s it!

Using command-line based vim to edit git commit messages on Mac OS X.

git config --global core.editor /usr/bin/vim

Source: Tooky

Resources

Workflows

1 URL schemes used by git

Subpages: