Versions Compared

Key

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

...

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>..." 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.py file2.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

...

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-control.rst
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#  examples/
file2.py

no changes added to commit (use "git add" and/or "git commit -a")

...

Code Block
$ git diff
diff --git a/version-controlfile2.rstpy b/version-controlfile2.rstpy
index e51819268bc8e4..b1c519a6b6dcff 100644
--- a/version-controlfile2.rstpy
+++ b/version-controlfile2.rstpy
@@ -243,61 +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 pythonprint ("This is file2.py")
+#!/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 a Remote Repository

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

...

Code Block
$ git pull origin master
  • Pushing changes

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

...

To fix this, 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:

...

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:

  • 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

The git log command shows you the history of your repository:

...

Code Block
$ git log
commit 7c8c3e71893d7481fdd9c13ec8f53cb9c61fac50
Author: testuser lastname  <testuser@g.harvard.edu>
Date:   Tue Sep 18 12:46:46 2018 -0400

    changed GNU to Microsoft

commit 257f2f3ff44c2165c1182d3673a825fcadf121aa
Author: testuser lastname  <testuser@g.harvard.edu>
Date:   Tue Sep 18 12:46:46 2018 -0400

    made a change

commit 99c4fb8f37e48284d79c7396aaf755b514d6a249
Author: testuser lastname  <testuser@g.harvard.edu>
Date:   Tue Sep 18 12:46:45 2018 -0400

    made some changes

commit 20cc63576f7c88541f5b9471e20f4d1c5f8afcb9
Author: testuser lastname  <testuser@g.harvard.edu>
Date:   Tue Sep 18 12:46:45 2018 -0400

    initial import
  • Tagging and branching

Git has explicit support for tagging and branching.

  • git tag manipulates tags
  • git branch and git checkout manipulate branches
  • Tags

Create a tag:

Code Block
$ git tag [-a] TAGNAME

...

To list available tags:

Code Block
$ git tag
  • Branches

List branches:

Code Block
$ git branch

...

Code Block
$ git checkout master
$ git merge 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 index
  • git 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:

...