Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Git Introduction: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 11: Line 11:
This section shows how to download the source code and explains the concept of a Git repository.
This section shows how to download the source code and explains the concept of a Git repository.


<code><br /> $ git clone git://gitorious.org/qt/qt.git<br /> cd qt<br /></code>
<code>
$ git clone git://gitorious.org/qt/qt.git
cd qt
</code>


This creates a new directory called “qt” and downloads the source code into it. It also downloads the entire history of all files in all versions, therefore creating an identical copy – called repository – of what is stored on the server. That is why the process is called cloning. The source of the clone is remembered as the “origin”.
This creates a new directory called “qt” and downloads the source code into it. It also downloads the entire history of all files in all versions, therefore creating an identical copy – called repository – of what is stored on the server. That is why the process is called cloning. The source of the clone is remembered as the “origin”.
Line 25: Line 28:
Run the log command to view recent changes:
Run the log command to view recent changes:


<code><br /> $ git log<br /></code>
<code>
$ git log
</code>


This command launches a pager program allowing you to scroll through the list of committed changes with the cursor and Page Up/Down keys. To return to the shell press the 'q' key.
This command launches a pager program allowing you to scroll through the list of committed changes with the cursor and Page Up/Down keys. To return to the shell press the 'q' key.
Line 31: Line 36:
The log command accepts options, such as -n to limit the number of changes. For example to list the last three changes run:
The log command accepts options, such as -n to limit the number of changes. For example to list the last three changes run:


<code><br /> $ git log -n3<br /></code>
<code>
$ git log -n3
</code>


To limit the output of the log command, e.g., to show only changes that affect one or multiple files/directories, you can use the following options:
To limit the output of the log command, e.g., to show only changes that affect one or multiple files/directories, you can use the following options:


<code><br /> $ git log src/corelib/kernel<br /> $ git log src/gui/kernel/qwidget.cpp<br /> $ git log examples/examples.pro examples/painting/basicdrawing/main.cpp<br /></code>
<code>
$ git log src/corelib/kernel
$ git log src/gui/kernel/qwidget.cpp
$ git log examples/examples.pro examples/painting/basicdrawing/main.cpp
</code>


There are various ways of limiting the output of the log command, beyond the ones described here. Refer to &quot;git log's documentation&amp;quot;:http://www.kernel.org/pub/software/scm/git/docs/git-log.html or more details, particularly the &quot;Commit Limiting&amp;quot; section.
There are various ways of limiting the output of the log command, beyond the ones described here. Refer to "git log's documentation":http://www.kernel.org/pub/software/scm/git/docs/git-log.html or more details, particularly the "Commit Limiting" section.


Changes in Git are identified with a unique SHA1 checksum. The checksum is on the first line of the git log output. For example:
Changes in Git are identified with a unique SHA1 checksum. The checksum is on the first line of the git log output. For example:


<code><br /> commit 211bea9838bcc2acd7f54b65468fe1be2d81b1e0<br /> Author: Denis Dzyubenko &lt;denis.dzyubenko</code>nokia.com&amp;gt;<br /> Date: Fri Apr 24 14:18:17 2009 ''0200
<code>
<br /> Re-send network request properly when the socket is in Closing state.
commit 211bea9838bcc2acd7f54b65468fe1be2d81b1e0
<br /> This fixes the &quot;QAbstractSocket::connectToHost() called when already<br /> connecting/connected to &lt;hostname&amp;gt;&quot;. The issue was that we were trying<br /> to call connect on a socket that was in a Closing state. We have to<br /> wait for the disconnected signal before we try to connect again.
Author: Denis Dzyubenko <denis.dzyubenko@nokia.com>
<br /> Reviewed-by: Prasanth<br /><code>
Date: Fri Apr 24 14:18:17 2009 ''0200
<br />You can inspect commits using the git show command with the commit id as argument. The following command shows the entire change above including a diff:
<br /></code><br /> $ git show 211bea9838bcc2acd7f54b65468fe1be2d81b1e0<br /><code>
<br />To see the last commit you made use show without any arguments:
<br /></code><br /> $ git show<br /><code>
<br />To inspect a whole series of commits, use:
<br /></code><br /> $ git log <s>p<br /><code>
<br />The git show and log commands allow you to inspect the history of the project or individual files. Sometimes it is also useful to inspect source code on a line-by-line basis, for example when you would like to find out who introduced a certain snippet of code. Git provides this functionality with the blame command:
<br /></code><br /> $ git blame src/gui/kernel/qwidget.cpp<br /><code>
<br />The output of this command</s> shown in a pager - is split into columns. The first column shows the commit id, followed by the author and date for each line.
<br />Git specializes in tracking the content of files, the name of a file is less important than the source code it contains. Therefore git blame can automatically detect, for example, that a function was moved from one file to another.
<br />h2. Making changes
<br />h3. Setting up for changes
<br />The previous sections explained how to clone the repository and how to find out the history of the project. This section finally shows how to create history, through the recording of changes in new commits and the tracking of commits in branches.
<br />The first step is to identify on what branch of development we are working. In Git, branches are a very lightweight concept that allows identifying commits by name. Each commit has a reference to its parent commit(s), which forms the history. The Git branch command, without any arguments, displays the list of local branches as well as the branch currently checked out:
<br /></code><br /> $ git branch<br /> * master<br /><code>
<br />The output shows that we have only one local branch called &quot;master&amp;quot; and the &quot;'''&quot; indicates that it is the current branch. The name &quot;master&amp;quot; in Git development usually describes the main line of development.
<br />Besides your local branches Git also remembers the branches in the origin repository. The &quot;-r&amp;quot; option shows them, prefixed with &quot;origin&amp;quot;:
<br /></code><br /> $ git branch <s>r<br /> origin/4.5<br /> origin/HEAD</s>&gt; origin/master<br /> origin/master<br /><code>
<br />For the purpose of developing a new feature we can create a new branch with the same command:
<br /></code><br /> $ git branch my-feature origin/master<br /><code>
<br />The &quot;my-feature&amp;quot; branch is based on the origin's &quot;master&amp;quot; branch, now visible in the list of branches:
<br /></code><br /> $ git branch<br />''' master<br /> my-feature<br /><code>
<br />The git checkout command can also change the current branch:
<br /></code><br /> $ git checkout my-feature<br /> Switched to branch &quot;my-feature&amp;quot;<br /> $ git branch<br /> master<br /> * my-feature<br /><code>
<br />This will also update the files in your working directory to match the latest commit in the new current branch. New commits will be recorded in &quot;my-feature&amp;quot; instead of the previous &quot;master&amp;quot; branch.
<br />You can have as many local branches as you like, they are very cheap. Each branch uses only few bytes of disk space. Please see the &quot;documentation of the git branch command&amp;quot;:http://git-scm.com/docs/git-branch for instructions on how to delete branches.
<br />h3. Actually making the changes
<br />Now we can go ahead and start making changes:
<br /></code><br /> $ edit src/corelib/tools/qstring.cpp<br /><code>
<br />The diff command shows us what we have changed:
<br /></code><br /> $ git diff<br /> diff —git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp<br /> index 375d672..d7345e6 100644<br /> — a/src/corelib/tools/qstring.cpp<br />'' b/src/corelib/tools/qstring.cpp<br /> &amp;#x40;&amp;#x40; –2855,6 ''2855,20 &amp;#x40;&amp;#x40; int QString::count(const QRegExp&amp;amp; rx) const<br /> Same as size().<br /> */
<br />''/*!<br /> + 4.6<br /> +<br /> + Returns this string in reverse order.<br /> '''''/<br /> ''QString QString::reversed() const<br />''{<br /> + QString result;<br /> + result.resize(d-&gt;size);<br /> + for (int i = 0; i &lt; d-&gt;size; +''i)<br />'' result[i] = d-&gt;data[d-&gt;size - 1 - i];<br /> + return result;<br /> ''}<br />''
<br /> /'''!<br /> QString::SectionFlag<br /> diff —git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h<br /> index 1493dce..077bda9 100644<br /> — a/src/corelib/tools/qstring.h<br />'' b/src/corelib/tools/qstring.h<br /> &amp;#x40;&amp;#x40; –195,6 ''195,8 &amp;#x40;&amp;#x40; public:<br /> int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;<br /> int count(const QString &amp;s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
<br />'' QString reversed() const;<br /> +<br /> #ifndef QT_NO_REGEXP<br /> int indexOf(const QRegExp &amp;, int from = 0) const;<br /> int lastIndexOf(const QRegExp &amp;, int from = –1) const;<br /><code>


The status command gives an overview of what &quot;git commit&amp;quot; would do:
Re-send network request properly when the socket is in Closing state.


</code><br /> $ git status<br /> # On branch master<br /> # Changed but not updated:<br /> # (use &quot;git add &lt;file&amp;gt;&quot; to update what will be committed)<br /> # (use &quot;git checkout — &lt;file&amp;gt;&quot; to discard changes in working directory)<br /> #<br /> # modified: src/corelib/tools/qstring.cpp<br /> # modified: src/corelib/tools/qstring.h<br /> #<br /> no changes added to commit (use &quot;git add&amp;quot; and/or &quot;git commit -a&amp;quot;)<br /><code>
This fixes the "QAbstractSocket::connectToHost() called when already
connecting/connected to <hostname>". The issue was that we were trying
to call connect on a socket that was in a Closing state. We have to
wait for the disconnected signal before we try to connect again.
 
Reviewed-by: Prasanth
</code>
 
You can inspect commits using the git show command with the commit id as argument. The following command shows the entire change above including a diff:
 
<code>
$ git show 211bea9838bcc2acd7f54b65468fe1be2d81b1e0
</code>
 
To see the last commit you made use show without any arguments:
 
<code>
$ git show
</code>
 
To inspect a whole series of commits, use:
 
<code>
$ git log -p
</code>
 
The git show and log commands allow you to inspect the history of the project or individual files. Sometimes it is also useful to inspect source code on a line-by-line basis, for example when you would like to find out who introduced a certain snippet of code. Git provides this functionality with the blame command:
 
<code>
$ git blame src/gui/kernel/qwidget.cpp
</code>
 
The output of this command- shown in a pager - is split into columns. The first column shows the commit id, followed by the author and date for each line.
 
Git specializes in tracking the content of files, the name of a file is less important than the source code it contains. Therefore git blame can automatically detect, for example, that a function was moved from one file to another.
 
h2. Making changes
 
h3. Setting up for changes
 
The previous sections explained how to clone the repository and how to find out the history of the project. This section finally shows how to create history, through the recording of changes in new commits and the tracking of commits in branches.
 
The first step is to identify on what branch of development we are working. In Git, branches are a very lightweight concept that allows identifying commits by name. Each commit has a reference to its parent commit(s), which forms the history. The Git branch command, without any arguments, displays the list of local branches as well as the branch currently checked out:
 
<code>
$ git branch
* master
</code>
 
The output shows that we have only one local branch called "master" and the "'''" indicates that it is the current branch. The name "master" in Git development usually describes the main line of development.
 
Besides your local branches Git also remembers the branches in the origin repository. The "-r" option shows them, prefixed with "origin":
 
<code>
$ git branch -r
origin/4.5
origin/HEAD-> origin/master
origin/master
</code>
 
For the purpose of developing a new feature we can create a new branch with the same command:
 
<code>
$ git branch my-feature origin/master
</code>
 
The "my-feature" branch is based on the origin's "master" branch, now visible in the list of branches:
 
<code>
$ git branch
''' master
my-feature
</code>
 
The git checkout command can also change the current branch:
 
<code>
$ git checkout my-feature
Switched to branch "my-feature"
$ git branch
master
* my-feature
</code>
 
This will also update the files in your working directory to match the latest commit in the new current branch. New commits will be recorded in "my-feature" instead of the previous "master" branch.
 
You can have as many local branches as you like, they are very cheap. Each branch uses only few bytes of disk space. Please see the "documentation of the git branch command":http://git-scm.com/docs/git-branch for instructions on how to delete branches.
 
h3. Actually making the changes
 
Now we can go ahead and start making changes:
 
<code>
$ edit src/corelib/tools/qstring.cpp
</code>
 
The diff command shows us what we have changed:
 
<code>
$ git diff
diff —git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index 375d672..d7345e6 100644
— a/src/corelib/tools/qstring.cpp
'' b/src/corelib/tools/qstring.cpp
&amp;#x40;&amp;#x40; –2855,6 ''2855,20 &amp;#x40;&amp;#x40; int QString::count(const QRegExp&amp;amp; rx) const
Same as size().
*/
 
''/*!
+ 4.6
+
+ Returns this string in reverse order.
''*/
''QString QString::reversed() const
''{
+ QString result;
+ result.resize(d->size);
+ for (int i = 0; i < d->size; +''i)
'' result[i] = d->data[d->size - 1 - i];
+ return result;
''}
''
 
/*!
QString::SectionFlag
diff —git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
index 1493dce..077bda9 100644
— a/src/corelib/tools/qstring.h
'' b/src/corelib/tools/qstring.h
&amp;#x40;&amp;#x40; –195,6 ''195,8 &amp;#x40;&amp;#x40; public:
int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
int count(const QString &amp;s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
 
'' QString reversed() const;
+
#ifndef QT_NO_REGEXP
int indexOf(const QRegExp &amp;, int from = 0) const;
int lastIndexOf(const QRegExp &amp;, int from = –1) const;
</code>
 
The status command gives an overview of what "git commit" would do:
 
<code>
$ 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: src/corelib/tools/qstring.cpp
# modified: src/corelib/tools/qstring.h
#
no changes added to commit (use "git add" and/or "git commit -a")
</code>


Following the recommendation in the output we mark qstring.cpp and .h as files to be included in the next commit:
Following the recommendation in the output we mark qstring.cpp and .h as files to be included in the next commit:


</code><br /> $ git add src/corelib/tools/qstring.cpp src/corelib/tools/qstring.h<br /> $ git status<br /> # On branch master<br /> # Changes to be committed:<br /> # (use &quot;git reset HEAD &lt;file&amp;gt;&quot; to unstage)<br /> #<br /> # modified: src/corelib/tools/qstring.cpp<br /> # modified: src/corelib/tools/qstring.h<br /> #<br /> # Untracked files not listed (use -u option to show untracked files)<br /><code>
<code>
$ git add src/corelib/tools/qstring.cpp src/corelib/tools/qstring.h
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>" to unstage)
#
# modified: src/corelib/tools/qstring.cpp
# modified: src/corelib/tools/qstring.h
#
# Untracked files not listed (use -u option to show untracked files)
</code>


And finally we create a new commit:
And finally we create a new commit:


</code><br /> $ git commit -m &quot;Add a new reversed() function to QString&amp;quot;<br /> [my-feature c8d9c54] Add a new reversed() function to QString<br /><code>
<code>
$ git commit -m "Add a new reversed() function to QString"
[my-feature c8d9c54] Add a new reversed() function to QString
</code>
 
Normally Git will open a new text editor if you omit the -m parameter.
 
Marking files to be included in commits means adding them to Git's staging area. The staging area is a powerful concept, making it easy to work on multiple files simultaenously while still creating small incremental commits.
 
Use the <code>git gui</code> command for a comfortable way to create highly selective commits.
 
h2. Common operations and fixing mistakes
 
This section gives recipes for fixing common mistakes and explains common operations such as reviewing your changes before committing using the diff command.
 
You can undo changes to a file before it is committed with the checkout command:
 
<code>
$ git checkout HEAD — src/corelib/tools/qstring.cpp
</code>
 
If you have accidentially marked a file for the next commit you can reset it like this:
 
<code>
$ git reset HEAD — src/corelib/tools/qstring.cpp
</code>
 
This will exclude the file from the next commit and leave it in your working directory unchanged with your modifications intact.
 
After changing a file the git diff command shows the lines you have modified. The difference is calculated from the file in your working directory and the file when it was added to the staging area using git add.
 
If you want to show the difference between the file in the staging area and the last commit you have to pass an option to the diff command:
 
<code>
$ git diff —staged # —cached in old git versions
</code>
 
The diff command optionally takes directories and files as argument, to limit the output.
 
Files are renamed in Git by renaming them in your working directory using a command/tool of your choice. Once you've renamed a file you have to tell Git:
 
<code>
[ rename oldfile.cpp to newfile.cpp using a graphical file manager ]
$ git rm oldfile.cpp
$ git add newfile.cpp
</code>
 
Alternatively you can use the convenience mv command:
 
<code>
$ git mv oldfile.cpp newfile.cpp
</code>
 
One of Git's strengths is that it does not care how exactly you change your files. It is optimized to find out what changed after the fact. For example when copying a file in your source code it is enough to simply run "git add" with the new file. Git automatically finds out where the file was copied from.
 
h2. Sending and updating your changes
 
After studying the previous chapters you should be able to develop small changes by creating commits in your local branches. This chapter guides you through the steps of combining your changes with the latest changes from the upstream repository and explains how to create separate patch files for submission into the project.
 
h3. Obtaining updates
 
While developing code in your local clone of the project, the original repository evolves and sees new changes. First we update our mirror of the commits in the original repository using the fetch command:


Normally Git will open a new text editor if you omit the <s>m parameter.
<code>
<br />Marking files to be included in commits means adding them to Git's staging area. The staging area is a powerful concept, making it easy to work on multiple files simultaenously while still creating small incremental commits.
$ git fetch
<br />Use the &lt;code&amp;gt;git gui&amp;lt;/code&amp;gt; command for a comfortable way to create highly selective commits.
remote: Counting objects: 3013, done.
<br />h2. Common operations and fixing mistakes
remote: Compressing objects: 100% (395/395), done.
<br />This section gives recipes for fixing common mistakes and explains common operations such as reviewing your changes before committing using the diff command.
remote: Total 1730 (delta 1455), reused 1582 (delta 1332)
<br />You can undo changes to a file before it is committed with the checkout command:
Receiving objects: 100% (1730/1730), 1.13 MiB | 392 KiB/s, done.
<br /></code><br /> $ git checkout HEAD — src/corelib/tools/qstring.cpp<br /><code>
Resolving deltas: 100% (1455/1455), completed with 892 local objects.
<br />If you have accidentially marked a file for the next commit you can reset it like this:
From git://gitorious.org/qt/qt.git
<br /></code><br /> $ git reset HEAD — src/corelib/tools/qstring.cpp<br /><code>
be7384d..822114e master-> origin/master
<br />This will exclude the file from the next commit and leave it in your working directory unchanged with your modifications intact.
</code>
<br />After changing a file the git diff command shows the lines you have modified. The difference is calculated from the file in your working directory and the file when it was added to the staging area using git add.
<br />If you want to show the difference between the file in the staging area and the last commit you have to pass an option to the diff command:
<br /></code><br /> $ git diff —staged # —cached in old git versions<br /><code>
<br />The diff command optionally takes directories and files as argument, to limit the output.
<br />Files are renamed in Git by renaming them in your working directory using a command/tool of your choice. Once you've renamed a file you have to tell Git:
<br /></code><br /> [ rename oldfile.cpp to newfile.cpp using a graphical file manager ]<br /> $ git rm oldfile.cpp<br /> $ git add newfile.cpp<br /><code>
<br />Alternatively you can use the convenience mv command:
<br /></code><br /> $ git mv oldfile.cpp newfile.cpp<br /><code>
<br />One of Git's strengths is that it does not care how exactly you change your files. It is optimized to find out what changed after the fact. For example when copying a file in your source code it is enough to simply run &quot;git add&amp;quot; with the new file. Git automatically finds out where the file was copied from.
<br />h2. Sending and updating your changes
<br />After studying the previous chapters you should be able to develop small changes by creating commits in your local branches. This chapter guides you through the steps of combining your changes with the latest changes from the upstream repository and explains how to create separate patch files for submission into the project.
<br />h3. Obtaining updates
<br />While developing code in your local clone of the project, the original repository evolves and sees new changes. First we update our mirror of the commits in the original repository using the fetch command:
<br /></code><br /> $ git fetch<br /> remote: Counting objects: 3013, done.<br /> remote: Compressing objects: 100% (395/395), done.<br /> remote: Total 1730 (delta 1455), reused 1582 (delta 1332)<br /> Receiving objects: 100% (1730/1730), 1.13 MiB | 392 KiB/s, done.<br /> Resolving deltas: 100% (1455/1455), completed with 892 local objects.<br /> From git://gitorious.org/qt/qt.git<br /> be7384d..822114e master</s>&gt; origin/master<br /><code>


The output indicates that the &quot;master&amp;quot; branch in the original gitorious.org repository has been updated with new commits and these commits are now available in the locally mirrored origin/master branch.
The output indicates that the "master" branch in the original gitorious.org repository has been updated with new commits and these commits are now available in the locally mirrored origin/master branch.


You can inspect the new changes using the log or show commands:
You can inspect the new changes using the log or show commands:


</code><br /> $ git log -p origin/master<br /><code>
<code>
$ git log -p origin/master
</code>


The log command will include the diffs of the individual commits if you specify the -p argument.
The log command will include the diffs of the individual commits if you specify the -p argument.
Line 129: Line 319:
To combine the new changes with our own local commits we use a technique called rebasing:
To combine the new changes with our own local commits we use a technique called rebasing:


</code><br /> $ git checkout my-feature<br /> $ git rebase origin/master<br /><code>
<code>
$ git checkout my-feature
$ git rebase origin/master
</code>


After making &quot;my-feature&amp;quot; the current branch, git rebase will first re-initialize the branch to be identical to origin/master, which includes the new commits. Then it will apply each of your commits, basing them on top of the newly initialized branch.
After making "my-feature" the current branch, git rebase will first re-initialize the branch to be identical to origin/master, which includes the new commits. Then it will apply each of your commits, basing them on top of the newly initialized branch.


=== Sending your feature for review ===
=== Sending your feature for review ===


When you decide that the work in your &quot;my-feature&amp;quot; branch is complete then the next step is to make your changes available for upstream inclusion.
When you decide that the work in your "my-feature" branch is complete then the next step is to make your changes available for upstream inclusion.


This is described in the [[Gerrit-Introduction]].
This is described in the [[Gerrit-Introduction]].
Line 141: Line 334:
== Conclusion ==
== Conclusion ==


We hope that this introductions helps you familiarize yourself with the use of Git. The official<br />&quot;Git documentation web page&amp;quot;:http://git-scm.com/documentation is a very good continuation point.
We hope that this introductions helps you familiarize yourself with the use of Git. The official
"Git documentation web page":http://git-scm.com/documentation is a very good continuation point.

Revision as of 09:29, 25 February 2015

h1. Developing and maintaining patches on top of Qt with Git

This document is a simple guide showing the procedures necessary to develop and maintain patches on top of Qt. It is not a replacement for the Git manual.

Setting up for Git and Gerrit

Setting-up-Gerrit explains how to configure git, and how to clone Qt repositories.

Getting the Qt source code

This section shows how to download the source code and explains the concept of a Git repository.

 $ git clone git://gitorious.org/qt/qt.git
 cd qt

This creates a new directory called “qt” and downloads the source code into it. It also downloads the entire history of all files in all versions, therefore creating an identical copy – called repository – of what is stored on the server. That is why the process is called cloning. The source of the clone is remembered as the “origin”.

You can create new clones simply by copying the directory, for example onto a USB stick. The new directory will be self-contained, too.

Now that you have all the changes inside the repository, you can work 100% offline. An internet connection is necessary only when you need to fetch new source code changes or push upstream.

Inspecting the source code

This section introduces the Git log, show and blame commands to find out the history of the project.

Run the log command to view recent changes:

 $ git log

This command launches a pager program allowing you to scroll through the list of committed changes with the cursor and Page Up/Down keys. To return to the shell press the 'q' key.

The log command accepts options, such as -n to limit the number of changes. For example to list the last three changes run:

 $ git log -n3

To limit the output of the log command, e.g., to show only changes that affect one or multiple files/directories, you can use the following options:

 $ git log src/corelib/kernel
 $ git log src/gui/kernel/qwidget.cpp
 $ git log examples/examples.pro examples/painting/basicdrawing/main.cpp

There are various ways of limiting the output of the log command, beyond the ones described here. Refer to "git log's documentation":http://www.kernel.org/pub/software/scm/git/docs/git-log.html or more details, particularly the "Commit Limiting" section.

Changes in Git are identified with a unique SHA1 checksum. The checksum is on the first line of the git log output. For example:

 commit 211bea9838bcc2acd7f54b65468fe1be2d81b1e0
 Author: Denis Dzyubenko <denis.dzyubenko@nokia.com>
 Date: Fri Apr 24 14:18:17 2009 ''0200

 Re-send network request properly when the socket is in Closing state.

 This fixes the "QAbstractSocket::connectToHost() called when already
 connecting/connected to <hostname>". The issue was that we were trying
 to call connect on a socket that was in a Closing state. We have to
 wait for the disconnected signal before we try to connect again.

 Reviewed-by: Prasanth

You can inspect commits using the git show command with the commit id as argument. The following command shows the entire change above including a diff:

 $ git show 211bea9838bcc2acd7f54b65468fe1be2d81b1e0

To see the last commit you made use show without any arguments:

 $ git show

To inspect a whole series of commits, use:

 $ git log -p

The git show and log commands allow you to inspect the history of the project or individual files. Sometimes it is also useful to inspect source code on a line-by-line basis, for example when you would like to find out who introduced a certain snippet of code. Git provides this functionality with the blame command:

 $ git blame src/gui/kernel/qwidget.cpp

The output of this command- shown in a pager - is split into columns. The first column shows the commit id, followed by the author and date for each line.

Git specializes in tracking the content of files, the name of a file is less important than the source code it contains. Therefore git blame can automatically detect, for example, that a function was moved from one file to another.

h2. Making changes

h3. Setting up for changes

The previous sections explained how to clone the repository and how to find out the history of the project. This section finally shows how to create history, through the recording of changes in new commits and the tracking of commits in branches.

The first step is to identify on what branch of development we are working. In Git, branches are a very lightweight concept that allows identifying commits by name. Each commit has a reference to its parent commit(s), which forms the history. The Git branch command, without any arguments, displays the list of local branches as well as the branch currently checked out:

 $ git branch
 * master

The output shows that we have only one local branch called "master" and the "" indicates that it is the current branch. The name "master" in Git development usually describes the main line of development.

Besides your local branches Git also remembers the branches in the origin repository. The "-r" option shows them, prefixed with "origin":

 $ git branch -r
 origin/4.5
 origin/HEAD-> origin/master
 origin/master

For the purpose of developing a new feature we can create a new branch with the same command:

 $ git branch my-feature origin/master

The "my-feature" branch is based on the origin's "master" branch, now visible in the list of branches:

 $ git branch
''' master
 my-feature

The git checkout command can also change the current branch:

 $ git checkout my-feature
 Switched to branch "my-feature"
 $ git branch
 master
 * my-feature

This will also update the files in your working directory to match the latest commit in the new current branch. New commits will be recorded in "my-feature" instead of the previous "master" branch.

You can have as many local branches as you like, they are very cheap. Each branch uses only few bytes of disk space. Please see the "documentation of the git branch command":http://git-scm.com/docs/git-branch for instructions on how to delete branches.

h3. Actually making the changes

Now we can go ahead and start making changes:

 $ edit src/corelib/tools/qstring.cpp

The diff command shows us what we have changed:

 $ git diff
 diff git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
 index 375d672..d7345e6 100644
  a/src/corelib/tools/qstring.cpp
'' b/src/corelib/tools/qstring.cpp
 &amp;#x40;&amp;#x40; 2855,6 ''2855,20 &amp;#x40;&amp;#x40; int QString::count(const QRegExp&amp;amp; rx) const
 Same as size().
 */

''/*!
 + 4.6
 +
 + Returns this string in reverse order.
 ''*/
 ''QString QString::reversed() const
''{
 + QString result;
 + result.resize(d->size);
 + for (int i = 0; i < d->size; +''i)
'' result[i] = d->data[d->size - 1 - i];
 + return result;
 ''}
''

 /*!
 QString::SectionFlag
 diff —git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
 index 1493dce..077bda9 100644
 — a/src/corelib/tools/qstring.h
'' b/src/corelib/tools/qstring.h
 &amp;#x40;&amp;#x40; –195,6 ''195,8 &amp;#x40;&amp;#x40; public:
 int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
 int count(const QString &amp;s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;

'' QString reversed() const;
 +
 #ifndef QT_NO_REGEXP
 int indexOf(const QRegExp &amp;, int from = 0) const;
 int lastIndexOf(const QRegExp &amp;, int from = –1) const;

The status command gives an overview of what "git commit" would do:

 $ 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: src/corelib/tools/qstring.cpp
 # modified: src/corelib/tools/qstring.h
 #
 no changes added to commit (use "git add" and/or "git commit -a")

Following the recommendation in the output we mark qstring.cpp and .h as files to be included in the next commit:

 $ git add src/corelib/tools/qstring.cpp src/corelib/tools/qstring.h
 $ git status
 # On branch master
 # Changes to be committed:
 # (use "git reset HEAD <file>…" to unstage)
 #
 # modified: src/corelib/tools/qstring.cpp
 # modified: src/corelib/tools/qstring.h
 #
 # Untracked files not listed (use -u option to show untracked files)

And finally we create a new commit:

 $ git commit -m "Add a new reversed() function to QString"
 [my-feature c8d9c54] Add a new reversed() function to QString

Normally Git will open a new text editor if you omit the -m parameter.

Marking files to be included in commits means adding them to Git's staging area. The staging area is a powerful concept, making it easy to work on multiple files simultaenously while still creating small incremental commits.

Use the

git gui

command for a comfortable way to create highly selective commits.

h2. Common operations and fixing mistakes

This section gives recipes for fixing common mistakes and explains common operations such as reviewing your changes before committing using the diff command.

You can undo changes to a file before it is committed with the checkout command:

 $ git checkout HEAD  src/corelib/tools/qstring.cpp

If you have accidentially marked a file for the next commit you can reset it like this:

 $ git reset HEAD  src/corelib/tools/qstring.cpp

This will exclude the file from the next commit and leave it in your working directory unchanged with your modifications intact.

After changing a file the git diff command shows the lines you have modified. The difference is calculated from the file in your working directory and the file when it was added to the staging area using git add.

If you want to show the difference between the file in the staging area and the last commit you have to pass an option to the diff command:

 $ git diff staged # cached in old git versions

The diff command optionally takes directories and files as argument, to limit the output.

Files are renamed in Git by renaming them in your working directory using a command/tool of your choice. Once you've renamed a file you have to tell Git:

 [ rename oldfile.cpp to newfile.cpp using a graphical file manager ]
 $ git rm oldfile.cpp
 $ git add newfile.cpp

Alternatively you can use the convenience mv command:

 $ git mv oldfile.cpp newfile.cpp

One of Git's strengths is that it does not care how exactly you change your files. It is optimized to find out what changed after the fact. For example when copying a file in your source code it is enough to simply run "git add" with the new file. Git automatically finds out where the file was copied from.

h2. Sending and updating your changes

After studying the previous chapters you should be able to develop small changes by creating commits in your local branches. This chapter guides you through the steps of combining your changes with the latest changes from the upstream repository and explains how to create separate patch files for submission into the project.

h3. Obtaining updates

While developing code in your local clone of the project, the original repository evolves and sees new changes. First we update our mirror of the commits in the original repository using the fetch command:

 $ git fetch
 remote: Counting objects: 3013, done.
 remote: Compressing objects: 100% (395/395), done.
 remote: Total 1730 (delta 1455), reused 1582 (delta 1332)
 Receiving objects: 100% (1730/1730), 1.13 MiB | 392 KiB/s, done.
 Resolving deltas: 100% (1455/1455), completed with 892 local objects.
 From git://gitorious.org/qt/qt.git
 be7384d..822114e master-> origin/master

The output indicates that the "master" branch in the original gitorious.org repository has been updated with new commits and these commits are now available in the locally mirrored origin/master branch.

You can inspect the new changes using the log or show commands:

 $ git log -p origin/master

The log command will include the diffs of the individual commits if you specify the -p argument.

Updating your branch with the downloaded, new changes

To combine the new changes with our own local commits we use a technique called rebasing:

 $ git checkout my-feature
 $ git rebase origin/master

After making "my-feature" the current branch, git rebase will first re-initialize the branch to be identical to origin/master, which includes the new commits. Then it will apply each of your commits, basing them on top of the newly initialized branch.

Sending your feature for review

When you decide that the work in your "my-feature" branch is complete then the next step is to make your changes available for upstream inclusion.

This is described in the Gerrit-Introduction.

Conclusion

We hope that this introductions helps you familiarize yourself with the use of Git. The official "Git documentation web page":http://git-scm.com/documentation is a very good continuation point.