Versions Compared

Key

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

...

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 rm file1.py

What's

...

modified: status

Use git status to see a list of modified files:

...

  • 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

...

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.

...

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

...

  • 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: