A Simple Git Workflow
I just started using Git for source control. Having used CVS and SVN extensively, I thought I had a decent background on version control and would be able to pick it up quickly. I quickly was overwhelmed by the amount of concepts and explanations needed in the documentation. Hint to the folks writing the Git book: start with simple concepts and build up from there. I don’t need to know how Git stores its files in the first chapter. I don’t need to know what a fast forward merge is. Give me one chapter that has a simple workflow example, then jump into the explanations of the internals and advanced features. I had to read through 50 pages of the book to figure out a basic remote workflow. Here are some of the bits I’ve gleaned. I’m still a Git beginner, so tips are always appreciated.
I’ll assume you have Git set up already, there are tutorials on the web for the wide variety of supported platforms. Once you’re set up with Git, you should be able to run ‘git –version’ at the command line and see a version string (like “git version 1.6.3.2″). All git commands are run in the format ‘git COMMAND_NAME OPTIONS’. Running ‘git –help’ will list all commands. Running ‘git help COMMAND_NAME’ will give a man page about that command. For example, to get information about the add command, you can run ‘git help add’.
Setting up a Git repository is simple. You can use an existing directory or create something from scratch. I’ll start out fresh. The ‘init’ command sets up a new repository.
Kleinschs-Macbook:tmp nick$ mkdir first && cd first Kleinschs-Macbook:first nick$ echo 'firstEdit' > file1 Kleinschs-Macbook:first nick$ git init |
Now we’re set up with a Git repository in this directory. The ‘status’ command gives you information about your working set.
Kleinschs-Macbook:first nick$ git status # On branch master # # Initial commit # # Untracked files: # (use "git add ..." to include in what will be committed) # # file1 nothing added to commit but untracked files present (use "git add" to track) |
To make a commit in Git, you first do an ‘add’ of all files you want to be included in the commit, then a ‘commit’. The commit command will prompt for a message using your favorite editor, or you can use the ‘-m’ flag to add the message at the command line.
Kleinschs-Macbook:first nick$ git add file1 Kleinschs-Macbook:first nick$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached ..." to unstage) # # new file: file1 # Kleinschs-Macbook:first nick$ git commit -m'First commit' [master (root-commit) 4cc0433] first file 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 file1 Kleinschs-Macbook:first nick$ git status # On branch master nothing to commit (working directory clean) |
Let’s say we want to make a change. We’ll modify the first file and add another.
Kleinschs-Macbook:first nick$ echo 'secondEdit' > file1 Kleinschs-Macbook:first nick$ echo 'anotherFile' > file2 Kleinschs-Macbook:first nick$ git status # On branch master # Changed but not updated: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: file1 # # Untracked files: # (use "git add ..." to include in what will be committed) # # file2 no changes added to commit (use "git add" and/or "git commit -a") |
Git detects that we’ve changed one file and added another. If you want to see the changes you made, you can use the ‘diff’ command to show a diff of the files.
Kleinschs-Macbook:first nick$ git diff file1 diff --git a/file1 b/file1 index dc2bdc4..1837f2f 100644 --- a/file1 +++ b/file1 @@ -1 +1 @@ -firstEdit +secondEdit |
We could add the change and commit, but there’s a slightly easier way. As the status output describes, you can use the ‘-a’ flag for commit to automatically commit all modified files. It will not add new files, you’ll still have to do add those yourself. Let’s add the second file and commit the whole thing.
Kleinschs-Macbook:first nick$ git add file2 Kleinschs-Macbook:first nick$ git commit -am'Made a second commit!' [master 74ec253] Made a second commit! 2 files changed, 2 insertions(+), 1 deletions(-) create mode 100644 file2 Kleinschs-Macbook:first nick$ git status # On branch master nothing to commit (working directory clean) |
The last thing to cover for right now is looking at past history. You use the ‘log’ command to view commit history.
Kleinschs-Macbook:first nick$ git log file1 commit 8ebf88b3d99309c1bd245156459a24549cf11ee8 Author: Nick Kleinschmidt <nick.kleinschmidt@gmail.com> Date: Sat Aug 15 09:28:26 2009 -0400 Made a second commit! commit 7801dc66b2b3b5029e888ad35e5f0405584b4ff1 Author: Nick Kleinschmidt <nick.kleinschmidt@gmail.com> Date: Sat Aug 15 09:24:47 2009 -0400 first commit Kleinschs-Macbook:first nick$ git log file2 commit 8ebf88b3d99309c1bd245156459a24549cf11ee8 Author: Nick Kleinschmidt <nick.kleinschmidt@gmail.com> Date: Sat Aug 15 09:28:26 2009 -0400 Made a second commit! |
With this setup, you can work locally, changing and committing to your hearts content. At this point, it’s fairly similar to most traditional version control systems. There’s still a lot to cover! Next time, I’ll get into setting up a remote repository, then we’ll dive into branching and merging.
