RSS

Category Archives: Git

Common Git commands

In my previous post last month I described the migration from SVN to Git. Since then I have been using it in my project. This post is about the typical workflow and how it can be achieved in Git.

1. Getting code

mkdir <dirname>
git init
git pull <repo URL>

This will pull the remote repository on your machine. The URL can be HTTP/HTTPS or SSH. In case of SSH you will need to generate a key pair first.

2. Seeing what has changed

git status

Shows the current branch you are on and the staged and unstaged changes.

3. Committing Changes

git commit -a -m <commit message>

Commits all the staged changes.

4. Pushing commits

git push

Push all commits to the remote repository

5. Storing current changes and going back to HEAD

git stash save <stash name>

All current changes are stashed away in a temporary area and resets the current branch

6. Restoring previously stored changes

git stash apply <stash name>

Merges the stashed changes back into the working copy

7. Create a new branch

git branch <branch name>

8. Switch to another branch

git checkout <branch name>

9. Merge branch with master

git checkout master
git merge <branch to merge>

10. Cherry pick changes from another branch

git cherry-pick -e -x <commit>

Pick specific commits, maybe from another branch, and apply to working copy

11. Tagging

git tag <tag name>

If you are new to Git you will find the Git GUI to be very handy. However, it does not support all the git commands such as stash. I always end up using both the command line and GUI at times when I get a bit lazy.

Advertisements
 
Leave a comment

Posted by on June 24, 2011 in Git

 

Moving from SVN to Git

I have been playing around with Git for a while now and its a great tool. The workflow is so much development oriented. So recently I decided to move one of my projects from SVN to Git, actually GitHub to be more specific. This post here details the steps in the process and lists some things to look out for. It assumes a basic understanding of Git. The Official Git Tutorial page is a good place to start. I found this talk by Linus Trovalds very insightful.

1. Register with GitHub

If you want to centrally host you Git repository on a server you can use GitHub. Its an excellent service for Git based projects with lots of features. It allows you to have both public and private hosted repositories. 

2. Clone the SVN repository

The first step in the process is to clone the SVN repository to a local Git repository. To do this run the following command

git svn clone -s <SVN Repo URL> <Git Repo Local Dir>

NOTE: Remove the -s option if you don’t have a standard SVN layout (tags, trunks, branches). You might need to use the --trunk, --tags, --branches options.

Depending on the size of the SVN repository and the commits in it this process might take a long time so go grab a coffee.

3. Check the Git repo

Now that the SVN repository is cloned into a  Git repository we can check the files in the new repository. Run the following command  to see commit logs. But we did not commit anything yet in Git, right? Well these are commits in SVN which Git has added into the new repository.

git log

4. Add from svn:ignore

If the SVN repository uses svn:ignore to ignore specific files, corresponding .gitignore files can be created as follows

git svn create-ignore
git add .
git commit --message "Committing gitignore files"

If the repository is to be published on a central server then proceed with the next steps otherwise you can jump to the What Next? section.

5. Push the Git repository to GitHub

You now have a local Git repository, ready to be pushed onto the GitHub server. But first things first. Generate a public/private keypair by following either of these guides based on your OS  Set Up Git (Windows)  and Set Up Git (Linux)

git remote add origin <SSH Git URL>
git push -u origin master

The SSH Git URL can be found by selecting the Git repository in GitHub and looking at the header as shown below

What Next?

Now you are all set and can start making changes to to your code and using the wonderful features of Git. But before that a few things to remember and understand.

  • Git does not track individual files but the contents
  • Git cannot have empty folders
  • A commit operation in Git commits to the local Git repository not the central repository. If you want to push it to the central repository then use git push.
  • Always run git status and git add before doing any commits to ensure that any new files are added. Alternatively you can use the -a switch in Git commit along with  the --interactive switch
  • Use branches whenever making changes. Its way too easy and a very central feature of Git.
  • Use git stash to store your changes into a working directory in case you need to restore to another branch temporarily.
 
4 Comments

Posted by on May 24, 2011 in Git

 
 
%d bloggers like this: