User Tools

Site Tools


wiki:git:start

Git

This page explains the use of Git and GitHub, and the most commonly used commands.


Git

Git is software for Revision Control. It is used to keep track of changes to your software project, maintain backups, facilitate collaboration with colleagues or the global community. You can use git without depending on an external server, but to share your repository with a wider audience, a git-server is necessary. GitHub is such a server, a popular commercial platform that provides free hosting of (public) git repositories.

Using GitHub

By putting your repository on Github, you make your software available to the global community. Anyone can modify your software in their own copy. If you like their improvements, you can merge it back into your own version.

  • Go to http://github.com and create an account.
  • On the top, next to your username, click the icon “Create repo” to create a new repository.
  • Give it a name
  • On your local machine, go to a directory where the repository can be copied

Clone

Clone the initial repository from Github

git clone https://$USERNAME@github.com/$USERNAME/$REPOSITORY.git
cd $REPOSITORY

You can clone to as many directories or computers as you like. If you give others access to your repository, by sharing it on github, they can clone it like this:

git clone https://$FRIEND_USERNAME@github.com/$USERNAME/$REPOSITORY.git

OPTIONAL: Config

Set your Name and Email address so commits show up properly on Github

# You can omit --global to get repository specific settings
# Enter the same email address as your github-account
git config --global name "John Doe"
git config --global email "johndoe@example.com"

Modify your files

Have fun and create or copy your files into the newly created directory.

Status

Check which files were modified or created

git status

Diff

Git can also show you exactly which lines were changed in each file.

# Add --color to show additions in green, and deletions in red.
git diff
git diff --color

Or, for a single file:

git diff --color example.txt

Add

Add your files to the commit-list. This way you can decide which files should be tracked and stored to the repository. Skip unnecessary files.

git add main.py
git add modules/myclass.py
git add docs/*

Add all files

Or: add all files in your directory. Both existing modified files and new files will be added.

git add -A

Reset (Undo add)

To remove a file from the commit list, use reset.

git reset modules/tmpfile

Commit

Commit your changes to your local repository. This creates a backup.

git commit -m "$COMMENT"

Add and commit

If you want to commit all previously added files, you can automatically add them to the current commit. New files will not be added.

git commit -am "$COMMENT"

Push

Push all your commits to GitHub.com. Before you can push, your repository must be up-to-date with respect to the repository on GitHub. Use git pull first if your repository is not updated.

git push # You need to enter your git password

Pull

Update your local repository from Github.

# Get all updates that were pushed from others
# (by friends who have access or from your other computers)
git pull

Your local edits will be automatically merged with edits on the remote repository (git calls this fastforward).

Merge conflict

After doing git pull, git normally automatically merges your edits with those on the remote repository.

If your edits conflict with the remote edits, then git cannot merge for you. Git will report which files had conflicts. Both your edits and remote edits will show up in the files. You need to reconcile these conflicting edits manually. Take a careful look and decide which lines should stay in the file and which shouldn't.

The file will contain a section similar to the following:

example.txt
<<<<<<< HEAD:mergetest
This is my third line
=======
This is a fourth line I am adding
>>>>>>> 4e2b407f501b68f8588aa645acafffa0224b9b78:mergetest

HEAD refers to the version on the remote server (GitHub). The section below the '======' is the edits you have made. Delete or rewrite the conflicting lines, remove the section indicators «« … »», ====== and re-add the file to the commit: git add example.txt. Use 'git status' to see if git reports any other merge conflicts.

wiki/git/start.txt · Last modified: 2013/01/29 13:01 by bouke