Archive:Development/Tutorials/Git: Difference between revisions

From KDE TechBase
m (style)
m (Reverted edits by KennethMartinez (talk) to last revision by AnneW)
 
(45 intermediate revisions by 18 users not shown)
Line 1: Line 1:
This tutorial will show you the basics for [http://git.or.cz/ Git]. For more information, please consult for example the official [http://www.kernel.org/pub/software/scm/git/docs/tutorial.html tutorial introduction to Git] or this [http://cheat.errtheblog.com/s/git excellent cheat sheet].
__NOINDEX__
 
{{warning| This page is obsolete. Proceed to [[Development/Git]].}}
 
== 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.
 
<pre>
git config --global user.name "Your Name"
git config --global user.email [email protected]
git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto
</pre>
 
In case you experience problems with colors you should test adding the following to you ~/.bashrc. The 'R' is the important part here.
 
<pre>
#needed for git colours
export LESS="-RIM"
</pre>
 
 
== First steps with Git ==
 
We will start with a new Git repository and add one file to it.
 
<pre>
carsten@moinmoin:~/git> git init
Initialized empty Git repository in .git/
carsten@moinmoin:~/git> echo "Test content" > testfile
</pre>
 
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.
 
<pre>
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)
</pre>
 
In the next three commands the file 'testfile' will be added and commited. Then  Git will check the status again.
 
<pre>
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)
</pre>
 
Ok, as you can see the file has been commited. Now let's see what we change the contents of the file:
 
<pre>
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(-)
</pre>
 
You see that Git noticed the changes in the file. <i>"git commit -a"</i> commits all changes in the repository. Note: This command does not add newly created files.
 
 
== Branches and merging are cheap in Git ==
 
<i>git branch</i> shows you the branches of the repository, the one with the '*' is the active one. So let us create a new branch called <i>"bugfix-branch"</i> 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.
 
<pre>
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
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
</pre>
 
Ok, the bug is fixed now. Next step: Checkout the master branch and merge the two branches:
 
<pre>
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
</pre>
 
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 ==
 
<pre>
carsten@moinmoin:~/git> git log testfile
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
</pre>
 
 
== Interfacing KDE's SVN repository with git-svn ==
 
<strong>Note:</strong> Please make sure to use Git 1.5.4 or better when you are interfacing with SVN. git-svn in older version has many issues you might run into otherwise.
 
<strong>Note:</strong> git-svn is not able to track most of the svn:properties yet. So, e.g. svn:externals are not fetched.
 
Here I'm going to explain to you how to fetch [http://edu.kde.org KDE-EDU]-trunk and import it into Git. I will then demonstrate how to make use of Git's features and sync with SVN again.
 
<pre>
git svn init https://svn.kde.org/home/kde/trunk/KDE/kdeedu
git svn fetch -r798745
</pre>
 
The revision has to be an existing revision of the module (to be found [http://websvn.kde.org/trunk/KDE/ here]).
 
To later update (sync with SVN) do:
 
<pre>
git svn rebase
</pre>
 
Now you can do everything you want, for example creating as many local branches as you like and merge back and forth. Whenever you want you can use this command to push your changes back into KDE's SVN repository:
 
<pre>
git svn dcommit
</pre>
 
Be warned, Git will create one SVN commit for each commit in your Git repository. To create just <b>one</b> commit for the whole merge of a branch into the master branch use the "--squash" feature like this:
 
<pre>
git merge --squash mybranch
</pre>
 
 
== Handling local changes ==
 
git-svn cannot sync with SVN when you have local, uncommited changes. For that you are using <i>git stash</i>. 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:
 
<pre>
git stash
git svn rebase
git stash apply
git stash clear
</pre>
 
If you have local changes which you would like to revert use the following command. It will revert all local, uncommited changes.
 
<pre>
git checkout -f
</pre>
 
 
== Final words ==
 
This tutorial is really just a start for Git. Another great Git-SVN tutorial is [http://live.gnome.org/GitForGnomeDevelopers this one].

Latest revision as of 15:48, 31 July 2012

Warning
This page is obsolete. Proceed to Development/Git.