...
Git is a distributed version control system developed by Linus Torvalds. It is used for managing Linux kernel development as well as for many other projects.
There are is a variet of excellent sources of documentationvariety of good information about Git available online, including:
- The official Git documentation, including which includes both a tutorial and a complete reference manual with links to individual subcommand documentation.
and the links from there for each sub-command. Also, refer to the git wiki at
Getting Started
Getting help
...
- Pro Git is an excellent free online book.
Getting Started
Getting help
Most commands have built-in documentation you can access with the --help option:
Code Block |
---|
git init --help |
Also available via manYou can generally access the same documentation as Unix man pages, e.g:
Code Block |
---|
man git-init |
Creating a repository
Use git init
to create a git repository in your current directory:
Code Block |
---|
$ mkdir myproject $ cd myproject $ git init |
Here the documentation for this command.
git init
git init
creates a git repository (named .git
) in your current working directory. You will add files to this repository using git add. This gives you a repository (the .git
directory) and a working copy (everything else).
Tracking an Existing
...
Project
If you are going to start tracking an existing project with git, you will generally start like this:
Code Block |
---|
$ cd myproject
$ git init
|
Initialized empty Git repository in .../.git/
This initializes the repository. Next, add the existing files to the repository:
Code Block |
---|
$ git add . $ git commit -m 'initial import' |
...
Adding files
The git add
command schedules files to be committed to the repository.
Code Block |
---|
$ git add init -b |
Adding files
The command git add
schedules files to be committed to the repository.
Code Block |
---|
git add PATH [PATH ...]
|
...
file1.c file2.c
|
Unlike Subversion, if you modify a file you (generally) need to git add that file in order to make the changes part of the next commit.
Use the git reset
command to "undo" an add operation:
...
This resets the index but leaves your working directory untouched. You can also use git reset
to revert to a previous commit; read the documentation for more information.
Committing changes
Use git commit
to commit files to your local repository:
Code Block |
---|
$ git commit [-a] [PATH ...] |
The command git commit
by itself will commit any changes scheduled using git add
. If you would like to commit all locally modified files, use the -a
option:
Code Block |
---|
$ git commit -a
|
You may also commit a subset of modified files by specifying paths on the
command line:
Code Block |
---|
$ git commit path/to/modified/file
|
...
Use git mv
to rename files in the repository:
Code Block |
---|
$ git mv SRColdname DSTnewname |
Because git
tracks files by cryptographic checksum, rather than by name, the git mv
command is not strictly necessary. If you manually rename a file and then do a git rm
file followed by a git add
file, git will correctly recognize that you have simply renamed it (because the checksum is still the same).
...
Use git rm
to remove files from the repository:
Code Block |
---|
$ git rm PATH [...]file1.c |
What's changed: status
Use git status to see a list of modified files:
Code Block |
---|
$ git status
|
The output of git status
will look something like this:
...
Use git diff to see pending changes in your working copy:
Code Block |
---|
$ git diff
|
The output of git diff is standard diff output, e.g.:
...
Code Block |
---|
git diff <commit1> <commit2> |
Working With Remote
...
Repositories
Cloning a Remote Repository
Use the git clone
command to check out a working copy of a remote repository:
Code Block |
---|
$ git clone REPOSITORY [DIRECTORY]
|
...
Use git pull
to update your local repository from the remote repository and merge changes into your working copy:
Code Block |
---|
$ git pull [REPOSITORY [REFSPEC]]
|
...
Use git push
to send your committed changes to a remote repository:
Code Block |
---|
$ git push [REPOSITORY [REFSPEC]] |
git push
by itself will push your changes to the remote repository defined by the branch.master.remote
config option (which will typically be the repository from which you originally cloned your working copy). If there are multiple remote repositories associated with your working copy, you can specify a repository (and branch) on the command line, e.g, to push your changes to branch master at a remote named origin:
Code Block |
---|
$ git push origin master |
Git doesn't like you pushing into a remote If you attempt to push to a repository that is associated with a working tree (because this could cause unexpected changes for the person who checked out that working tree). You will generally want to create "bare" repositories for remote access (using git init --bare
).If you attempt to push to a repository that is newer than your working copy you will see an error similar to the followingnewer than your working copy you will see an error similar to the following:
Code Block |
---|
$ git push To dottiness.seas.harvard.edu:repos/myproject ! [rejected] master -> master (non-fast forward) error: failed to push some refs to 'dottiness.seas.harvard.edu:repos/myproject' To fix this, run git pull and deal with any conflicts. |
To fix this, you need to pull from that repo, merge changes, and then push, you need to pull from that repo, merge changes, and then push.
Sharing your repository
If you will be sharing a repository with others (or with yourself on multiple computers), you will need to create a "bare" repository – that is, a repository without a working copy. You do this with the -b
flag to git init
:
Code Block |
---|
$ git init -b
|
You can then clone this repository, pull from it, and push to it as described in the previous section.
Conflicts
A conflict occurrs when two people make overlapping changes.
...
To resolve the conflict manually:
- (1) Edit the conflicting files as necessary.
To discard your changes (and accept the remote repository version):
- (2) run
git checkout --theirs README
To override the repository with your changes:
- (3) run
git checkout --ours README
When you complete the above tasks:
- (4) add the files with
git add
- (5) commit the changes with
git commit
.
...
The git log command shows you the history of your repository:
Code Block |
---|
$ git log [PATH] |
git log
with no arguments shows you the commit messages for each revision in your repository:
...
Tags
Create a tag:
Code Block |
---|
$ git tag [-a] TAGNAME
|
Creates a lightweight tag (an alias for a commit object). Add -a
to create an annotated tag (i.e., with an associated message). It is also possible to create cryptographically signed tags.
To list available tags:
Code Block |
---|
git tag
|
To get information about a specific tag:
Code Block |
---|
$ git tag -v TAGNAME |
Branches
List branches:
Code Block |
---|
$ git branch |
Create a new branch rooted at START:
Code Block |
---|
$ git checkout branch-b BRANCHNAME [START] |
...
To switch to a branch:
Code Block |
---|
git checkout BRANCHNAME |
To create a branch rooted at START and switch to it:
Code Block |
---|
$ git checkout -b BRANCHNAME [START] |
For example, you want to enhance your code with some awesome experimental code. You create a new seas-workshop-dev branch and switch to it:
...