Git TutorialJae Woo Lee and Stephen A. EdwardsColumbia UniversityMarch 2013Git is a source code version control system. Such a system is most useful when you work in ateam, but even when you’re working alone, it’s a very useful tool to keep track of the changesyou have made to your code.Configure your git environmentTell git your name and email:git config --global "Your Full Name"git config --global [email protected] stores this information in the /.gitconfig file.You may also want to set the EDITOR environment variable to vim or emacs; this controlswhich editor you use to enter log messages.Creating a projectLet’s create a new directory, /tmp/test1, for our first git project.cdmkdir tmpcd tmpmkdir test1cd test1Put the directory under git revision control:git initIf you type ll (I’ll assume that ll is an alias for ls -alF), you will see that there is a .gitdirectory. The git repository for the current directory is stashed in the .git directory.Let’s start our programming project. Write hello.c with your editor:

#include stdio.h int main(){printf("%s\n", "hello world");return 0;}Compile and run it:gcc hello.c./a.outLet’s see what git thinks about what we’re doing:git statusThe git status command reports that hello.c and a.out are “Untracked.” We can have gittrack hello.c by adding it to the “staging” area (more on this later):git add hello.cRun git status again. It now reports that hello.c is “a new file to be committed.” Let’scommit it:git commitGit opens up your editor for you to type a commit message. A commit message shouldsuccinctly describe what you’re committing in the first line. If you have more to say, followthe first line with a blank line, and then with a more through multi-line description.For now, type in the following one-line commit message, save, and exit the editor.Added hello-world program.Run git status again. It now reports that only a.out is untracked. It has no mention ofhello.c. When git says nothing about a file, it means that it is being tracked, and that it hasnot changed since it has been last committed.We have successfully put our first coding project under git revision control.Modifying filesModify hello.c to print “bye world” instead, and run git status. It reports that the file is“Changed but not updated.” This means that the file has been modified since the last commit,but it is still not ready to be committed because it has not been moved into the staging area.In git, a file must first go to the staging area before it can be committed.Before we move it to the staging area, let’s see what we changed in the file:

git diffOr, if your terminal supports color,git diff --colorThe output should tell you that you took out the “hello world” line, and added a “bye world”line, like this:-printf("%s\n", "hello world"); printf("%s\n", "bye world");We move the file to the staging area with git add command:git add hello.cIn git, “add” means this: move the change you made to the staging area. The change could bea modification to a tracked file, or it could be a creation of a brand new file. This is a pointof confusion for those of you who are familiar with other version control systems such asSubversion.At this point, git diff will report no change. Our change—from hello to bye—has beenmoved into staging already. So this means that git diff reports the difference between thestaging area and the working copy of the file.To see the difference between the last commit and the staging area, add --cached option:git diff --cachedLet’s commit our change. If your commit message is a one-liner, you can skip the editor bygiving the message directly as part of the git commit command:git commit -m "changed hello to bye"To see your commit history:git logYou can add a brief summary of what was done at each commit:git log --stat --summaryOr you can see the full diff at each commit:git log -pAnd in color:git log -p --color

The tracked, the modified, and the stagedA file in a directory under git revision control is either tracked or untracked. A tracked filecan be unmodified, modified but unstaged, or modified and staged. Confused? Let’s try again.There are four possibilities for a file in a git-controlled directory:1. UntrackedObject files and executable files that can be rebuilt are usually not tracked.2. Tracked, unmodifiedThe file is in the git repository, and it has not been modified since the last commit.git status says nothing about the file.3. Tracked, modified, but unstagedYou modified the file, but didn’t git add the file. The change has not been staged, so it’snot ready for commit yet.4. Tracked, modified, and stagedYou modified the file, and did git add the file. The change has been moved to thestaging area. It is ready for commit.The staging area is also called the “index.”Other useful git commandsHere are some more git commands that you will find useful.To rename a tracked file:git mv old-filename new-filenameTo remove a tracked file from the repository:git rm filenameThe mv or rm actions are automatically staged for you, but you still need to git commit youractions.Sometimes you make some changes to a file, but regret it, and want to go back to the versionlast committed. If the file has not been staged yet, you can do:git checkout -- filenameIf the file has been staged, you must first unstage it:git reset HEAD filename

There are two ways to display a manual page for a git command. For example, for thegit status command, you can type one of the following two commands:git help statusman git-statusLastly, git grep searches for specified patterns in all files in the repository. To see all placesyou called printf():git grep printfCloning a projectYou created a brand new project in the test1 directory, added a file, and modified the file.But more often than not, a programmer starts with an existing code base. When the codebase is under git version control, you can clone the whole repository.Let’s move up one directory, clone test1 into test2, and cd into the test2 directory:cd .git clone test1 test2cd test2Type ll to see that your hello.c file is cloned here. Moreover, if you run git log, you willsee that the whole commit history is replicated here. git clone not only copies the latestversion of the files, but also copies the entire repository, including the entire commit history.After cloning, the two repositories are indistinguishable.Let’s make some changes—and let’s be bad. Edit hello.c to replace printf with printf% &,save and commit:vim hello.cgit add hello.cgit commit -m "hello world modification - work in progress"Now run git log to see your recent commit carrying on the commit history that was cloned.If you want to see only the commits after cloning:git log origin.Of course you can add -p and --color to see the full diff in color:git log -p --color origin.Let’s make one more modification. Fix the printf, and perhaps change the “bye world” to“rock my world” while we’re there.

vim hello.cgit add hello.cgit commit -m "fixed typo & now prints rock my world"Run git log -p --color origin. again to see the two commits you have made after cloning.Adding a directory into your repositoryEnter the original test1 directory, create the solution subdirectory, and add two files to it:cd ./test1mkdir solutioncd solutioncp ./hello.c .echo ’hello:’ MakefileType ll to see that two files—Makefile and hello.c—have been created in the solutionsdirectory. (hello.c was copied from the parent directory, and Makefile was created directlyon the command line using the echo command.Now, move out of the solution directory, and git add & git commit the solution directory:cd .git add solutiongit commit -m "added solution"Note that git add solution stages all files in the directory.Pulling changes from a remote repositoryWhen others are working on the same project, you will often want to get updated files from aremote repository. You do that by “pulling” the changes in my repository into your repository.Let’s pull the changes we just made in test1 into test2:cd ./test2/git pullThe git pull command looks at the original repository that you cloned from, fetches all thechanges made since the cloning, and merges the changes into the current repository. Younow have the solution right in your repository.Learning more about gitman gittutorialThe documentation page of the Git web site,, also hasmany links.