Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If you are creating a repository that people will be accessing remotely, you will normally want to create a "bare" repository, which consists of just the contents of the .git directory and no working copy. You do this with the -b flag:

...

Code Block
git add PATH [PATH ...]

documentation

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.

...

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.

...

Code Block
git commit [-a] [PATH ...]

documentation

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 path/to/modified/file

h1. 

Managing

...

git mv SRC DST

Code Block


[documentation]

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).

h2. Removing files

Use git rm to remove files from the repository:

git rm PATH ...

Code Block


[documentation]

git: What's changed: status

Use git status to see a list of modified files:

git status

Code Block


[documentation]

The output of git status will look something like this:

$ git status

...

Code Block


The files listed as "changed but not updated" are files that you have modified but not yet added to the repository. "Untracked files" are files that have not previously been added to the repository.

h2. What's changed: diffs

Use git diff to see pending changes in your working copy:

...

Files

Renaming files

Use git mv to rename files in the repository:

Code Block

git mv SRC DST

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).

Removing files

Use git rm to remove files from the repository:

Code Block

git rm PATH [...]

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:

Code Block

$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#  modified:   version-control.rst
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#  examples/
no changes added to commit (use "git add" and/or "git commit -a")

The files listed as "changed but not updated" are files that you have modified but not yet added to the repository. "Untracked files" are files that have not previously been added to the repository.

What's changed: diffs

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

...


diff --git a/version-control.rst b/version-control.rst

...


index e518192..b1c519a

...

 100644
--- a/version-control.rst

...


+++ b/version-control.rst

...


@@ -243,6 +243,34 @@ commit`` to commit them to the (local) repository::

...


 Using git: What's changed?

...


 ==========================

...



+Use ``git status`` to see a list of modified files::

...


+

...


+  git status

...


+

...


+.. container:: handout

...


+

...


+   The output will look something like this::

...


+

You can also use git diff to see the changes between arbitrary revisions of your project:

  • Changes in working copy vs. previous commit:
    Code Block
    git diff <commit>

-Changes between two previous commits:

Code Block
git diff <commit1> <commit2>

...

Working With Remote Repos

Cloning a

...

Remote Repository

Use the git clone command to check out a working copy of a remote repository:git

Code Block

git clone REPOSITORY [DIRECTORY]

documentationThe command git clone will clone the remote repository to a new directory in your current directory named after the repository, unless you explicitly provide a name with the DIRECTORY argument.

...

You can only clone the top-level repository; unlike Subversion, git does not allow you to clone individual subtrees.git:

Updating your working copy

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]]

...


git pull by itself will pull changes from 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 pull changes from the branch master at a remote named origin:

Code Block

$ git pull origin master

...

Pushing changes

Use git push to send your committed changes to a remote repository:

Code Block
git push [REPOSITORY [REFSPEC]]

documentationgit 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 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 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.

Conflicts

A conflict occurrs when two people make overlapping changes.

...