Difference between revisions of "Development/Tutorials/Git/Basics"

Jump to: navigation, search
(Fix typoes)
(Remove carsten's username and machine from the prompts to genericise the instructions)
Line 35: Line 35:
  
 
<code>
 
<code>
carsten@moinmoin:~/git> git init
+
$ git init
 
Initialized empty Git repository in .git/
 
Initialized empty Git repository in .git/
carsten@moinmoin:~/git> echo "Test content" > testfile
+
$ echo "Test content" > testfile
 
</code>
 
</code>
  
Line 43: Line 43:
  
 
<code>
 
<code>
carsten@moinmoin:~/git> git status
+
$ git status
 
# On branch master
 
# On branch master
 
#
 
#
Line 58: Line 58:
  
 
<code>
 
<code>
carsten@moinmoin:~/git> git add testfile
+
$ git add testfile
carsten@moinmoin:~/git> git commit -m "This is the first commit"
+
$ git commit -m "This is the first commit"
 
Created initial commit 246d7aa: This is the first commit
 
Created initial commit 246d7aa: This is the first commit
 
  1 files changed, 1 insertions(+), 0 deletions(-)
 
  1 files changed, 1 insertions(+), 0 deletions(-)
 
  create mode 100644 testfile
 
  create mode 100644 testfile
carsten@moinmoin:~/git> git status
+
$ git status
 
# On branch master
 
# On branch master
 
nothing to commit (working directory clean)
 
nothing to commit (working directory clean)
Line 71: Line 71:
  
 
<code>
 
<code>
carsten@moinmoin:~/git> echo "new content" > testfile
+
$ echo "new content" > testfile
carsten@moinmoin:~/git> git status
+
$ git status
 
# On branch master
 
# On branch master
 
# Changed but not updated:
 
# Changed but not updated:
Line 80: Line 80:
 
#
 
#
 
no changes added to commit (use "git add" and/or "git commit -a")
 
no changes added to commit (use "git add" and/or "git commit -a")
carsten@moinmoin:~/git> git commit -a -m "Second commit"
+
$ git commit -a -m "Second commit"
 
Created commit 14a9802: Second commit
 
Created commit 14a9802: Second commit
 
  1 files changed, 1 insertions(+), 1 deletions(-)
 
  1 files changed, 1 insertions(+), 1 deletions(-)
Line 92: Line 92:
  
 
<code>
 
<code>
carsten@moinmoin:~/git> git branch
+
$ git branch
 
* master
 
* master
carsten@moinmoin:~/git> git branch bugfix-branch
+
$ git branch bugfix-branch
carsten@moinmoin:~/git> git checkout bugfix-branch
+
$ git checkout bugfix-branch
 
Switched to branch "bugfix-branch"
 
Switched to branch "bugfix-branch"
carsten@moinmoin:~/git> git branch
+
$ git branch
 
* bugfix-branch
 
* bugfix-branch
 
   master
 
   master
carsten@moinmoin:~/git> echo "a second file" > newfile
+
$ echo "a second file" > newfile
carsten@moinmoin:~/git> git commit -a
+
$ git commit -a
 
# On branch bugfix-branch
 
# On branch bugfix-branch
 
# Untracked files:
 
# Untracked files:
Line 108: Line 108:
 
#      newfile
 
#      newfile
 
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)
carsten@moinmoin:~/git> git add newfile
+
$ git add newfile
carsten@moinmoin:~/git> git commit
+
$ git commit
 
Created commit 3264357: This file is here for a demonstration of Gits branch- and merge feature
 
Created commit 3264357: This file is here for a demonstration of Gits branch- and merge feature
 
  1 files changed, 1 insertions(+), 0 deletions(-)
 
  1 files changed, 1 insertions(+), 0 deletions(-)
Line 118: Line 118:
  
 
<code>
 
<code>
carsten@moinmoin:~/git> git checkout master
+
$ git checkout master
 
Switched to branch "master"
 
Switched to branch "master"
carsten@moinmoin:~/git> ls
+
$ ls
 
testfile
 
testfile
carsten@moinmoin:~/git> git merge bugfix-branch
+
$ git merge bugfix-branch
 
Updating 14a9802..3264357
 
Updating 14a9802..3264357
 
Fast forward
 
Fast forward
Line 128: Line 128:
 
  1 files changed, 1 insertions(+), 0 deletions(-)
 
  1 files changed, 1 insertions(+), 0 deletions(-)
 
  create mode 100644 newfile
 
  create mode 100644 newfile
carsten@moinmoin:~/git> ls
+
$ ls
 
newfile  testfile
 
newfile  testfile
 
</code>
 
</code>
Line 137: Line 137:
  
 
<code>
 
<code>
carsten@moinmoin:~/git> git log testfile
+
$ git log testfile
 
commit 14a9802e249413003d1fa40002baa025aa54c75f
 
commit 14a9802e249413003d1fa40002baa025aa54c75f
 
Author: Carsten Niehaus <carsten@moinmoin.site>
 
Author: Carsten Niehaus <carsten@moinmoin.site>

Revision as of 02:50, 2 November 2009

This tutorial will show you the basics for Git.

Contents

Setting up Git

First, you should tell Git your name and email address. These information will be shown in the log and in commits. Also, you should allow color in Git. There are other color-related features, but this tutorial is just about basics.

git config --global user.name "Your Name" git config --global user.email you@example.com git config --global color.ui true

In case you experience problems with colors you should test adding the following to your ~/.bashrc. The 'R' is the important part here.

  1. R needed for git colours

export LESS="-RIM"

First steps with Git

There are two ways to get started with Git for KDE. If you're only wanting to get code to try it out, you can use the following commands:

git clone <REPOSITORY URL>

This creates a local copy of the upstream repository. Now change into that directory and code away. When you want to update:

git pull

Later, if you want to create a new Git repository and add files to it, try the preceeding commands:

$ git init Initialized empty Git repository in .git/ $ echo "Test content" > testfile

Now we will check the status of the repository. Git will list one untracked file, that means the file has not yet been added to the repository.

$ 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 the file 'testfile' will be added and commited. Then Git will check the status again.

$ git add testfile $ git commit -m "This is the first commit" Created initial commit 246d7aa: This is the first commit

1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 testfile

$ git status

  1. On branch master

nothing to commit (working directory clean)

Ok, as you can see the file has been commited. Now let's see what we change the contents of the file:

$ echo "new content" > testfile $ 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") $ git commit -a -m "Second commit" 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. Note: This command does not add newly created files.

Branches and merging are cheap in Git

git branch shows you the branches of the repository, the one with the '*' is the active one. So let us 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) we will merge back all the hard work into the master branch.

$ git branch

  • master

$ git branch bugfix-branch $ git checkout bugfix-branch Switched to branch "bugfix-branch" $ git branch

  • bugfix-branch
 master

$ echo "a second file" > newfile $ 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) $ git add newfile $ git commit 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:

$ git checkout master Switched to branch "master" $ ls testfile $ git merge bugfix-branch Updating 14a9802..3264357 Fast forward

newfile |    1 +
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 newfile

$ ls newfile testfile

If you would have edited "testfile" in the bugfix-branch, then git would automatically try to merge the contents of "testfile" in the bugfix-branch with the contents of "testfile" in the master branch. Sometimes this can cause a merge conflict. In that case you have to manually edit the "testfile" in the master branch, and afterwards you do a "git commit -a" to complete the merge. Git indicates the conflicting lines in the file itself.

Lets now have a look at the log of the testfile

$ git log testfile commit 14a9802e249413003d1fa40002baa025aa54c75f Author: Carsten Niehaus <carsten@moinmoin.site> Date: Fri Apr 18 18:07:18 2008 +0200

   Second commit

commit 246d7aad05139314e7ff62a5becb6c930f72fb8f Author: Carsten Niehaus <carsten@moinmoin.site> Date: Fri Apr 18 18:06:33 2008 +0200

   This is the first commit

Handling local changes

git-svn cannot sync with SVN when you have local, uncommited changes. For that you are using git stash. That command move the local changes on a stack so that you can sync. After the sync you re-apply them to you Git tree and clear the stack. Very handy feature in many situations! Just do this:

git stash git svn rebase git stash apply git stash clear

If you have local changes which you would like to revert use the following command. It will revert all local, uncommited changes.

git checkout -f


KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal