Many people consider Git to be too confusing or complex to be a choice for version control. Yet Git considers to grow in adoption, and many interesting things have grown up around it. This document is geared for someone wanted to get started with Git, often coming from a Subversion background. For most basic needs this document will cover 70 to 90 percent of your use.
To use Git you will have to setup a repository. You can take an existing directory to make a Git repository, or create an empty directory.
To make your current directory a Git repository we simply run
To make a new directory that is a Git repository we just specify a directory.
git init newrepo
From here on out we’ll assume that you are in the root of the Git repository unless otherwise noted.
Adding New Files
So we have a repository, but nothing in it. You can add files with the
git add filename
To add everything in your directory try
git add ..
Committing a Version
Now that we’ve added these files, we want them to actually be stored in the Git repository. We do this by committing them to the repository.
git commit -m "Adding files"
If you leave off the
-m you will be put into an editor to write the message yourself.
When you’ve made changes to some files, you can run
git status to see what will happen on commit. You’ll notice a list of modified files, and a message:
no changes added to commit (use "git add" and/or "git commit -a")
git commit will do nothing unless you explicitly add files to the commit with
git add. If you’re looking for the commit command to automatically commit local modifications we use the
git commit -a -m "Changed some files"
Or if you’d like to have only certain files, but still not run
git add we pass specific files.
git commit -m "change some files" file1 file2
Do note that
-a will not cause new files to be committed, only modified.
Publishing Your Repository
To put your repository on a server we’ll start by making a “bare” repository, and upload it to a server.
cd /tmp git clone --bare ~/your/repo/path project.git scp -r project.git ssh://example.com/~/www/
Now if we have a couple of commits and want to push it up to that location:
git push ssh://example.com/~/www/project.git
If you dislike typing the URI each time we can take advantage that a cloned project remembers where it came from.
cd .. git clone ssh://example.com/~/www/project.git project
git push will push to the URI it was cloned from. You can do this manually by editing
.git/config in your repository.
Get Upstream Changes
If you’re already setup for push as above:
Will bring changes down and merge them in. To pull from a non-default location just specify the URI.
git pull http://git.example.com/project.git
More Than Five Minutes
You’ll have noticed that Git thinks in “commits.” These are uniquely identified by a hash. You can see the history and the hashes with
git log. Each commit involves modifications, new files, and files being removed.
add will put a file in a commit.
git reset HEAD will remove everything from the planned commit, but not change files.
If you want to remove a file from the repository, removing it from future commits we use
git rm file
Branching and Merging
Branches are done locally and are fast. To create a new branch we use the
git branch test
the branch command does not move us into the branch, just create one. So we use the
checkout command to change branches.
git checkout test
The first branch, or main branch, is called “master.”
git checkout master
While in your branch you can commit changes that will not be reflected in the master branch. When you’re done, or want to push changes to master, switch back to master and use
git checkout master git merge test
And if you’re done with the branch you can delete with the
branch command and pass the
git branch -d test
Traveling Through Time
You can quickly very previous states of the repository using the
checkout command again.
git checkout HASH
Uncommited changes will travel with you. Return to the preset with
git checkout master as with normal branches. If you commit while in the past a branch is automatically created and your changes will have to be merged forward.
Sweeping Changes Under the Rug for Later
When moving between branches your local changes move with you. Sometimes you want to switch branches but not commit or take those changes with you. The Git command
stash lets you put changes into a safe store.
You can retreive by passing an arguement of
git stash apply
The difference between
pop is simple.
apply will take a stash state and apply it, but preserve that state in the stash.
pop will take the stash state, apply it, and remove it from the stash.
git stash clear empties the contents of the stash.