Child pages
  • Git

Versions Compared

Key

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

...

More in-depth Git documentation can be found on the official home page, which mentions books, videos, and links to other tutorials and references. Furthermore, the shell command git help lists the most commonly used Git commands, and git help <command> gives very detailed documentation for the specified Git command.

Contents

Table of Contents
maxLevel32
stylecircle

Creating Commits

...

The master branch should look like this:

Other Useful Commands

This section contains optional steps that you don't need to push online, but can be useful for you to learn.

Ignoring Files

While working on his Machine, Alan Turing has produced a temporary file experiments.tmp, which he does not want to commit in the repository:

No Format
$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       experiments.tmp
nothing added to commit but untracked files present (use "git add" to track)

Since the extra mention of that file can make Git's status reports unnecessarily cluttered, Alan wants to ignore it permanently. Help him by adding a .gitignore file to the repository:

No Format
$ echo "*.tmp" > .gitignore
$ git add .gitignore
$ git commit -m "added ignore file"
[master 738ce4c] added ignore file
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)

Now the experiments.tmp file is not considered when viewing the status. You can add arbitrary file name patterns to the .gitignore file; for example it is a good idea to ignore *.class, which are binary files generated for Java projects.

Discarding Changes

While working on his Machine, Alan Turing has made some changes to notes.txt that he later found out to be nonsense:

No Format
$ 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:   notes.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

Help Alan by restoring the last committed state of that file:

No Format
$ git checkout HEAD notes.txt
$ git status
# On branch master
nothing to commit (working directory clean)

Instead of HEAD, which is the last commit on the current branch, you can also name any other branch or commit hash. In that case you would have to commit the change to make it permanent. While resolving conflicts it is possible to use --theirs or --ours instead of HEAD, which replaces the whole content of the respective file by their version (the one on the remote branch) or our version (the one on the current branch).

A more brute-force option is using the reset command:

No Format
$ git reset --hard
HEAD is now at b58ded7 Merge branch 'master' of git.rtsys.informatik.uni-kiel.de:personal-msp/turing

This resets all changes to the working copy to the head of the current branch, so use it with caution!

Rebasing