Initializing a Repository in an Existing Directory
git initCloning an Existing Repository
If you want to get a copy of an existing Git repository—for example, a project you’d like to
contribute to—the command you need is git clone
You clone a repository with git clone [URL]. For example, if you want to clone the Ruby
Git library called Grit, you can do so like this:
git clone git://github.com/sonam/grit.git
That creates a directory named grit,initializes a *git directory inside it, pulls down all
the data for that repository, and checks out a working copy of the latest version.
Recording Changes to the Repository
You have a bona fide Git repository and a checkout or working copy of the files for that project.
Remember that each file in your working directory can be in one of two states: tracked or
untracked. Tracked files are files that were in the last snapshot; they can be unmodified, modi-
fied, or staged. Untracked files are everything else—any files in your working directory that
were not in your last snapshot and are not in your staging area. When you first clone a repository,
all of your files will be tracked and unmodified because you just checked them out and haven’t
edited anything.
Checking the Status of Your Files
git status
Tracking New Files
git add [name of file]
Skipping the Staging Area
git commit -a -m "message"
Viewing the Commit History
After you have created several commits, or if you have cloned a repository with an existing
commit history, you’ll probably want to look back to see what has happened.
git log
or
git log --stat
One of the more helpful options is -p which shows the diff introduced in each commit.
You can also use -2 ,which limits the output to only the last two entries:
git log -p -2
Undoing Things
At any stage, you may want to undo something. Here, I’ll review a few basic tools for undoing
changes that you have made. Be careful, because you can’t always undo some of these undos.
This is one of the few areas in Git where you may lose some work if you do it wrong.
Changing Your Last Commit
One of the common undos takes place when you commit too early and possibly forget to add
some files, or you mess up your commit message. If you want to try that commit again, you
can run commit with the --amend option:
git commit --amend
This command takes your staging area and uses it for the commit.
And then you can add and commit that file.
Adding Remote Repositories
git remote add [URL]
Fetching and Pulling from Your Remotes
git fetch [remote-name]
Pushing to Your Remotes
git push origin master
Listing Your Tags
git tag
Creating Tags
Git uses two main types of tags: lightweight and annotated. A lightweight tag is very much like
a branch that does not change—it’s just a pointer to a specific commit. Annotated tags, how-
ever, are stored as full objects in the Git database. They’re check-summed; contain the tagger
name, e-mail, and date; have a tagging message; and can be signed and verified with GNU Pri-
vacy Guard (GPG). It’s generally recommended that you create annotated tags so you can have
all this information; but if you want a temporary tag or for some reason don’t want to keep the
other information, lightweight tags are available too.
Annotated Tags
Creating an annotated tag in Git is simple. The easiest way is to specify -a when you run the
tag command:
git tag -a [name of tag] -m "message"
Signed Tags
You can also sign your tags with GPG, assuming you have a private key.
git tag -s [name of tag] -m "message"
Lightweight Tags
Another way to tag commits is with a lightweight tag.This is basically the commit checksum
stored in a file—no other information is kept.
git tag [name of tag] -lw
Verifying Tags
To verify a signed tag
git tag -v [name of tag]
Sharing Tags
The git push command does not transfer tags to remote servers. You have to explic-
itly push tags to a shared server after you create them. This process is just like sharing remote
branches—you can run
git push origin [name of tag]
If you have a lot of tags that you want to push up at once
git push origin --tag
No comments:
Post a Comment