...
There is a variety of good information about Git available online, including:including The official Git documentation, which includes both a tutorial and a complete reference manual with links to individual subcommand documentation
...
.
Getting Started
Getting help
...
This initializes the repository. Next, create two files to add the existing files to the repository:to the repo
Code Block |
---|
$ cat > file1.py << EOF
cat > file1.py << EOF
> #!/usr/bin/env python
> print ("This is file1.py")
> EOF
$ cat > file2.py << EOF
> #!/usr/bin/env python
> print ("This is file2.py")
> EOF |
What files added? status
Code Block |
---|
$ git status On branch master Initial commit Untracked files: (use "git add <file>. $ git commit -m 'initial import'.." to include in what will be committed) file1.py file2.py nothing added to commit but untracked files present (use "git add" to track) |
Adding files
The git add
command schedules files to be committed to the repository.
Code Block |
---|
$ git add file1.cpy 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.
py
|
Use the git reset
command (if you want ) to "undo" an add operation:
...
You may also commit a subset of modified files by specifying paths on the
command line:
Code Block |
---|
$ git commit [path/to/modified/file] -m 'brief message of the recent change' |
Managing Files
Renaming files
...
Code Block |
---|
$ git rm file1.cpy |
What's
...
modified: status
Use git status to see a list of modified files:
Code Block |
---|
$ cat > file2.py << EOF
> #!/usr/bin/env python
> print ("This is file2.py")
> print ("New line ")
> EOF |
Code Block |
---|
$ git status |
The output of git status
will look something like this:
Code Block |
---|
$ git status # On branch master #Changes Changednot butstaged notfor updatedcommit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: version-controlfile2.rst # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # examples/ py 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.
...
Code Block |
---|
$ git diff diff --git a/version-controlfile2.rstpy b/version-controlfile2.rstpy index e518192b9a7216..b1c519a6b6dcff 100644 --- a/version-controlfile2.rstpy +++ b/version-controlfile2.rstpy @@ -2431,62 +2431,343 @@ 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:: + #!/usr/bin/env python print ("This is file2.py") +print ("New line ") |
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 Repositories
Cloning
...
Remote Repository
Use the git clone
command to check out a working copy of a remote repository:
...
The 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.
This is analogous to Subversion's checkout operation.
You can only clone the top-level repository; unlike Subversion, git does not allow you to clone individual subtrees.
Updating your working copy
Use git pull
to update your local repository from the remote repository and merge changes into your working copy:
...
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.seascode.harvard.edu:repos/myproject ! [rejected] master -> master (non-fast forward) error: failed to push some refs to 'dottiness.seascode.harvard.edu:repos/myproject' To fix this, run git pull and deal with any conflicts. |
...
Code Block |
---|
$ git pull remote: Counting objects: 5, done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 2), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From /Users/testuser/projects/version-control-workshop/work/repo2repo 4245cb6..84f1112 master -> origin/master Auto-merging README CONFLICT (content): Merge conflict in README Automatic merge failed; fix conflicts and then commit the result. |
To resolve the conflict manually:
- Edit the conflicting files as necessary.
To discard your changes (and accept the remote repository version):
- run
git checkout --theirs README
To override the repository with your changes:
- run
git checkout --ours README
When you complete the above tasks:
- add the files with
git add
- commit the changes with
git commit
.
Log, Tags, and Branches
Viewing history
...
Code Block |
---|
$ git log commit 7c8c3e71893d7481fdd9c13ec8f53cb9c61fac50 Author: testuser lastname <testuser@seas<testuser@g.harvard.edu> Date: ThuTue MarSep 18 12:46:46 20102018 -0400 changed GNU to Microsoft commit 257f2f3ff44c2165c1182d3673a825fcadf121aa Author: testuser lastname <testuser@seas<testuser@g.harvard.edu> Date: ThuTue MarSep 18 12:46:46 20102018 -0400 made a change commit 99c4fb8f37e48284d79c7396aaf755b514d6a249 Author: testuser lastname <testuser@seas<testuser@g.harvard.edu> Date: ThuTue MarSep 18 12:46:45 20102018 -0400 made some changes commit 20cc63576f7c88541f5b9471e20f4d1c5f8afcb9 Author: testuser lastname <testuser@seas<testuser@g.harvard.edu> Date: ThuTue MarSep 18 12:46:45 20102018 -0400 initial import |
Tagging and branching
Git has explicit support for tagging and branching.
git tag
manipulates tagsgit branch
andgit checkout
manipulate branches
Tags
Create a tag:
Code Block |
---|
$ git tag [-a] TAGNAME |
...
For example, you want to enhance your code with some awesome experimental code. You create a new seas-workshop-dev branch "dev" branch and switch to it:
Code Block |
---|
$ git checkout -b seas-workshop-dev
|
You make some changes, and when things are working you commit your branch:
...
Code Block |
---|
$ git checkout master
$ git merge seas-workshop-dev
Updating 1288ed3..33e4a4c
Fast-forward
version-control.rst | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)
|
The Git Index
Git is not really just like Subversion (or most other version control solutions). That's mainly because of the git "index".
- The index is a staging area between your working copy and your local repository.
git add
adds files to the indexgit commit
commits files from the index to the repository.
The diff commands uses the index:
git diff
is the difference between your working copy and the index.git diff HEAD
is the difference between your working copy and the local repository.git diff --cached
is the difference between the index and the local repository.
Refer back to this illustration if you get confused: