Archive:Development/Tutorials/Git

Revision as of 16:43, 18 April 2008 by 91.97.0.120 (talk | contribs) (Initial Version)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Setting up Git

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

[email protected]:~/git> git init Initialized empty Git repository in .git/ [email protected]:~/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.

[email protected]:~/git> git status

  1. On branch master
  2. Initial commit
  3. Untracked files:
  4. (use "git add <file>..." to include in what will be committed)
  5. 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.

[email protected]:~/git> git add testfile [email protected]:~/git> git commit Created initial commit 246d7aa: This is the first commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 testfile [email protected]:~/git> git status

  1. 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:

[email protected]:~/git> echo "new content" > testfile [email protected]:~/git> git status

  1. On branch master
  2. Changed but not updated:
  3. (use "git add <file>..." to update what will be committed)
  4. modified: testfile

no changes added to commit (use "git add" and/or "git commit -a") [email protected]:~/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.

[email protected]:~/git> git-branch

  • master

[email protected]:~/git> git branch bugfix-branch [email protected]:~/git> git checkout bugfix-branch Switched to branch "bugfix-branch" [email protected]:~/git> git branch

  • bugfix-branch

master [email protected]:~/git> echo "a second file" > newfile [email protected]:~/git> git commit -a

  1. On branch bugfix-branch
  2. Untracked files:
  3. (use "git add <file>..." to include in what will be committed)
  4. newfile

nothing added to commit but untracked files present (use "git add" to track) [email protected]:~/git> git add newfile [email protected]:~/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:

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


Lets now have a look at the log of the testfile

[email protected]:~/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


Content is available under Creative Commons License SA 4.0 unless otherwise noted.