Archive:Development/Tutorials/Git: Difference between revisions

From KDE TechBase
(Initial Version)
 
(pre, not blockquote)
Line 3: Line 3:
First, I started a new Git repository and added one file to it.
First, I started a new Git repository and added one file to it.


<blockquote>
<pre>
carsten@moinmoin:~/git> git init
carsten@moinmoin:~/git> git init
Initialized empty Git repository in .git/
Initialized empty Git repository in .git/
carsten@moinmoin:~/git> echo "Test content" > testfile
carsten@moinmoin:~/git> echo "Test content" > testfile
</blockquote>
</pre>


Now I will check what the status is. I will list one untracked file, that means the file has not yet been added to the repository.
Now I will check what the status is. I will list one untracked file, that means the file has not yet been added to the repository.


<blockquote>
<pre>
carsten@moinmoin:~/git> git status
carsten@moinmoin:~/git> git status
# On branch master
# On branch master
Line 22: Line 22:
#      testfile
#      testfile
nothing added to commit but untracked files present (use "git add" to track)
nothing added to commit but untracked files present (use "git add" to track)
</blockquote>
</pre>


In the next three commands I will add the file, commit the file and then check for the status again.
In the next three commands I will add the file, commit the file and then check for the status again.


<blockquote>
<pre>
carsten@moinmoin:~/git> git add testfile
carsten@moinmoin:~/git> git add testfile
carsten@moinmoin:~/git> git commit
carsten@moinmoin:~/git> git commit
Line 35: Line 35:
# On branch master
# On branch master
nothing to commit (working directory clean)
nothing to commit (working directory clean)
</blockquote>
</pre>


Ok, as you can see the file has been commited. Now I will demonstrate what happens when I am changine the contents of the file:
Ok, as you can see the file has been commited. Now I will demonstrate what happens when I am changine the contents of the file:


<blockquote>
<pre>
carsten@moinmoin:~/git> echo "new content" > testfile
carsten@moinmoin:~/git> echo "new content" > testfile
carsten@moinmoin:~/git> git status
carsten@moinmoin:~/git> git status
Line 52: Line 52:
Created commit 14a9802: Second commit
Created commit 14a9802: Second commit
  1 files changed, 1 insertions(+), 1 deletions(-)
  1 files changed, 1 insertions(+), 1 deletions(-)
</blockquote>
</pre>


You see that Git noticed the changes in the file. "git-commit -a" commits all changes in the repository.
You see that Git noticed the changes in the file. "git-commit -a" commits all changes in the repository.
Line 59: Line 59:
git-branch shows you the branches of the repository, the one with the '*' is the active one. So lets create a new branch called "bugfix-branch" and assume we want to fix a branch there. After this fix (in this case the new file) I will merge back all the hard work into the master branch.
git-branch shows you the branches of the repository, the one with the '*' is the active one. So lets create a new branch called "bugfix-branch" and assume we want to fix a branch there. After this fix (in this case the new file) I will merge back all the hard work into the master branch.


<blockquote>
<pre>
carsten@moinmoin:~/git> git-branch
carsten@moinmoin:~/git> git-branch
* master
* master
Line 81: Line 81:
  1 files changed, 1 insertions(+), 0 deletions(-)
  1 files changed, 1 insertions(+), 0 deletions(-)
  create mode 100644 newfile
  create mode 100644 newfile
</blockquote>
</pre>


Ok, the bug is fixed now. Next step: Checkout the master branch and merge the two branches:
Ok, the bug is fixed now. Next step: Checkout the master branch and merge the two branches:


<blockquote>
<pre>
carsten@moinmoin:~/git> git checkout master
carsten@moinmoin:~/git> git checkout master
Switched to branch "master"
Switched to branch "master"
Line 98: Line 98:
carsten@moinmoin:~/git> ls
carsten@moinmoin:~/git> ls
newfile  testfile
newfile  testfile
</blockquote>
</pre>






<h3>Lets now have a look at the log of the testfile</h3>
<h3>Lets now have a look at the log of the testfile</h3>
<blockquote>
<pre>
carsten@moinmoin:~/git> git log
carsten@moinmoin:~/git> git log
commit 14a9802e249413003d1fa40002baa025aa54c75f
commit 14a9802e249413003d1fa40002baa025aa54c75f
Line 116: Line 116:


     This is the first commit
     This is the first commit
</blockquote>
</pre>

Revision as of 16:46, 18 April 2008

Setting up Git

First, I started a new Git repository and added one file to it.

carsten@moinmoin:~/git> git init
Initialized empty Git repository in .git/
carsten@moinmoin:~/git> echo "Test content" > testfile

Now I will check what the status is. I will list one untracked file, that means the file has not yet been added to the repository.

carsten@moinmoin:~/git> git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       testfile
nothing added to commit but untracked files present (use "git add" to track)

In the next three commands I will add the file, commit the file and then check for the status again.

carsten@moinmoin:~/git> git add testfile
carsten@moinmoin:~/git> git commit
Created initial commit 246d7aa: This is the first commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 testfile
carsten@moinmoin:~/git> git status
# On branch master
nothing to commit (working directory clean)

Ok, as you can see the file has been commited. Now I will demonstrate what happens when I am changine the contents of the file:

carsten@moinmoin:~/git> echo "new content" > testfile
carsten@moinmoin:~/git> git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   testfile
#
no changes added to commit (use "git add" and/or "git commit -a")
carsten@moinmoin:~/git> git commit -a
Created commit 14a9802: Second commit
 1 files changed, 1 insertions(+), 1 deletions(-)

You see that Git noticed the changes in the file. "git-commit -a" commits all changes in the repository.

Branches are cheap in Git

git-branch shows you the branches of the repository, the one with the '*' is the active one. So lets create a new branch called "bugfix-branch" and assume we want to fix a branch there. After this fix (in this case the new file) I will merge back all the hard work into the master branch.

carsten@moinmoin:~/git> git-branch
* master
carsten@moinmoin:~/git> git branch bugfix-branch
carsten@moinmoin:~/git> git checkout bugfix-branch
Switched to branch "bugfix-branch"
carsten@moinmoin:~/git> git branch
* bugfix-branch
  master
carsten@moinmoin:~/git> echo "a second file" > newfile
carsten@moinmoin:~/git> git commit -a
# On branch bugfix-branch
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       newfile
nothing added to commit but untracked files present (use "git add" to track)
carsten@moinmoin:~/git> git add newfile
carsten@moinmoin:~/git> git commit -a
Created commit 3264357: This file is here for a demonstration of Gits branch- and merge feature
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 newfile

Ok, the bug is fixed now. Next step: Checkout the master branch and merge the two branches:

carsten@moinmoin:~/git> git checkout master
Switched to branch "master"
carsten@moinmoin:~/git> ls
testfile
carsten@moinmoin:~/git> git merge bugfix-branch
Updating 14a9802..3264357
Fast forward
 newfile |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 newfile
carsten@moinmoin:~/git> ls
newfile  testfile


Lets now have a look at the log of the testfile

carsten@moinmoin:~/git> git log
commit 14a9802e249413003d1fa40002baa025aa54c75f
Author: Carsten Niehaus <[email protected]>
Date:   Fri Apr 18 18:07:18 2008 +0200

    Second commit

commit 246d7aad05139314e7ff62a5becb6c930f72fb8f
Author: Carsten Niehaus <[email protected]>
Date:   Fri Apr 18 18:06:33 2008 +0200

    This is the first commit