Friday, August 3, 2012

Quick list of Git commands for SVN users

Here I provide a quick list of Git commands and how they compare to SVN. I hope this list will be useful for SVN users that are starting to use Git.

Creating a copy of the remote repository

Supose that you have created a repository to host your code in a hosting service such as GitHub or Bitbucket. The first thing you probably want to do is to create a local copy of your repository:

$ git clone [url]

This will create a local copy of the remote repository and allow you to look, use and modify the code. Note that differently from SVN, where only the latest revision of the repository is copied, the git clone command stores locally the entire repository, including all its revisions.

Commiting code

The very useful git status command can be used to check which files where modified, created or deleted:

$ git status

Before the git commit command is called, it is necessary to inform which files are to be included or updated. To do that use the git add command:

$ git add [file name]

Note that even files that are already being tracked and were modified must be included using the git add command before the commit. If you have a big list of files to commit and you don’t want to add them individually, it is possible to use wildcards. For example, running the following command in the parent directory of your repository will add all files listed by the git status command:

$ git add .

The git status command can be used to check if the files were correctly added. To commit the added files use the following command:

$ git commit –m "[commit message]"

Additionally, it is possible list which files would be commited by the git command without making any changes to your repository using the following command:

$ git commit [-n|--dry-run]

It is very important to note that when you run git commit only the local repository is updated. For example, if you are using a hosting service (e.g. GitHub) it will not change the remote repository. To send your changes it is necessary to use the git push command.

Sending the changes to the remote repository

To send the changes that you made in your local repository using the git commit command use git push:

$ git push –u [alias] [branch]

The [alias] argument is an alias to the remote repository and [branch] describes to which branch of the remote repository you are sending the changes. If you are not working with branches, you can just replace [branch] by master. If your remote repository alias is origin, the git push command will be:

$ git push –u origin master

If you don’t know the alias for the remote repository just run the git remote command:

$ git remote

It will return the remote repository alias. An interesting difference between Git and SVN is that in Git it is possible to make multiples commit locally and then, when you want to share your modifications, you can use the git push command to send the updates that you made.

Updating the local repository

If there were modifications in the remote repository, made either by you or someone in your team, you will probably want to update your local repository. To do that you can use the git pull command which is the equivalent of the svn update command:

$ git pull origin master

Discarding changes

Sometimes it may be useful to discard changes made locally. As suggested in this stackoverflow post, a quick way to do this is:

$ git stash save --keep-index
$ git stash drop

Further reference

Here I only give a list of basic Git commands. For more detailed information you can look at the official documentation. This site also provides a quick reference to Git commands.

If you intend to use Git on an open-source project, this blog post by Mark Berger is highly recommended.