...
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 | ||||
---|---|---|---|---|
|
Creating Commits
...
In the previous section you have created two commits on the default branch, which is named master
. Now you will create a new branch and commit there, thus adding complexity to the commit graph. In general, you may create as many local branches as you like, since they are simple to use and can be a great tool to structure your work.
Create a branch with name sketches:
No Format $ git branch sketches
View the list of branches:
No Format $ git branch * master sketches
The star reveals that you are still on the old
master
branch.Switch to the new branch:
No Format $ git checkout sketches Switched to branch 'sketches' $ git branch master * sketches
It is also possible to create a branch and switch immediately to it using the option
-b
ofgit checkout
.Download and add the new file
examples.txt
:No Format $ git add examples.txt $ git commit -m "wrote first examples" [sketches cd63135] wrote first examples 1 files changed, 20 insertions(+), 0 deletions(-) create mode 100644 examples.txt
Inspecting the commit graph with
gitk
(or another graphical viewer) you see that thesketches
branch now has three commits, whilemaster
is still at the second commit.Merging the
sketches
branch intomaster
means that all changes that have been made insketches
are also applied tomaster
. In order to perform this merge, we have to check out themaster
branch first:No Format $ git checkout master Switched to branch 'master' $ git merge sketches Updating 52e2d49..cd63135 Fast-forward examples.txt | 20 ++++++++++++++++++++ 1 files changed, 20 insertions(+), 0 deletions(-) create mode 100644 examples.txt
This was a fast-forward merge: since the
master
branch was completely contained in thesketches
branch, the merge could be done by simply changing the head pointer ofmaster
to be the same as the head ofsketches
.Now add the line "
see some examples in 'examples.txt'
" to the filenotes.txt
and commit this change in the current branch:No Format $ git add notes.txt $ git commit -m "added reference to the new examples" [master a5e244f] added reference to the new examples 1 files changed, 2 insertions(+), 1 deletions(-)
Switch back to the
sketches
branch and modify it as shown below. Note that thecheckout
command modifies your working copy, hence you have to update your text editor's content if you opened one of the files.No Format $ git checkout sketches Switched to branch 'sketches'
Add the line "
Move one step left:
" and write followed by an accordingly updated version of the tape with tape head in the at the end of the fileexamples.txt
, then commit.No Format $ git add examples.txt $ git commit -m "added another example" [sketches 55a9cb1] added another example 1 files changed, 5 insertions(+), 0 deletions(-)
Now your two branches have diverged, which means that they cannot be fast-forwarded anymore.
Merge the
master
branch intosketches
:No Format $ git merge master Merge made by recursive. notes.txt | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
Using
gitk
you can see that a new commit was created that has two parent commits. Such a commit is called merge commit and is done automatically when a non-fast-forward merge is applied. See how both the change tonotes.txt
done in themaster
branch and the change toexamples.txt
done in thesketches
branch are now contained in the repository state that results from the merge.- Add a commit in each of the two branches using the commands you have already learned.
- Check out
master
. Insert the following line after line 4 of
notes.txt
:No Format nopanel true * The finite state machine has an initial state and one or more final states
- Commit the change of
notes.txt
. - Check out
sketches
(make sure to refresh your text editor so thatnotes.txt
is reset to its previous state, without the change made above). Insert the following line after line 4 of
notes.txt
:No Format nopanel true * Each state transition can trigger head movement and data read/write
- Commit the change of
notes.txt
.
- Check out
Merge the
master
branch into the current branch (sketches
):No Format $ git merge master Auto-merging notes.txt CONFLICT (content): Merge conflict in notes.txt Automatic merge failed; fix conflicts and then commit the result.
As expected, the branches could not be merged automatically, since both branches modified the same line in the same file.
Use the
status
command to see the list of affected files:No Format $ git status # On branch sketches # Unmerged paths: # (use "git add/rm <file>..." as appropriate to mark resolution) # # both modified: notes.txt # no changes added to commit (use "git add" and/or "git commit -a")
The modified
notes.txt
should now contain the following text:No Format nopanel true <<<<<<< HEAD * Each state transition can trigger head movement and data read/write ======= * The finite state machine has an initial state and one or more final states >>>>>>> master
The upper line is the one committed to
sketches
, while the lower line was committed tomaster
. You have to resolve the conflict by editing the file. In this case the conflict is resolved by keeping both lines in arbitrary order, that means you should just remove the conflict markers (lines 5, 7, and 9 innotes.txt
).Use the
add
command to marknotes.txt
as resolved. Enteringgit commit
without a message will open a text editor with an automatically created commit message. Just close the editor, and the merge commit is completed:No Format $ git commit [sketches 21d5ddb] Merge branch 'master' into sketches $ git show 21d5ddb commit 21d5ddbbcba4e36464653a2a550dbf595ead921f Merge: 17f75c7 8af2d50 Author: Miro Spoenemann <msp@informatik.uni-kiel.de> Date: Tue Oct 16 10:44:09 2012 +0200 Merge branch 'master' into sketches Conflicts: notes.txt diff --cc notes.txt index 8f72873,bb81298..ba94a08 --- a/notes.txt +++ b/notes.txt @@@ -2,6 -2,6 +2,7 @@@ * Tape head can read or write data * Tape head can move left or right * The head is controlled by a finite state machine + * Each state transition can trigger head movement and data read/write + * The finite state machine has an initial state and one or more final states see some examples in 'examples.txt'
...
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! However, reset
does not remove unstaged files. In order to do that in one command, use clean
:
No Format |
---|
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test1.tmp
# test2.tmp
nothing added to commit but untracked files present (use "git add" to track)
$ git clean -f
Removing test1.tmp
Removing test2.tmp |
Rebasing
Consider the following situation:
If you want to merge the changes made on the master
branch into the sketches
branch, the normal way is to use the merge
command and create a merge commit. However, the rebase
command gives an interesting alternative to that: it reapplies all commits done in the current branch starting from a given reference.
No Format |
---|
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: added another example
Applying: state transitions
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging notes.txt |
Afterwards the commit graph looks like this:
The two commits made in sketches
are reapplied starting from the head of the master
branch. The resulting structure of commits is much cleaner than before. rebase
even allows to squeeze multiple commits into one. Note that in this example a merge conflict had to be resolved in the same way as it was done in Section "Branching and Merging"; instead of committing the resolved file, the rebase command is resumed with git rebase --continue
.
Warning |
---|
Never rebase a branch that is already pushed online! Due to the structural change the rebased branch is no longer compatible with the previous one, and pushing it will fail, since fast-forward merge is not possible. |
Tagging
Finally Alan Turing has made a great success in the development of his Machine, and he would like to fix that stage as "Milestone 1". Help him by tagging the current state of the project:
No Format |
---|
$ git tag milestone1 |
Then the head of the current branch is stored under the name milestone1
, so it can be found very easily at later stages of the project:
No Format |
---|
$ git tag
milestone1
$ git checkout milestone1
Note: checking out 'milestone1'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at 957f686... Merge remote branch 'gitorious/master' |
Tags can also be loaded to the server using the push
command.