Development/Tutorials/Git/Basics: Difference between revisions

From KDE TechBase
No edit summary
m (Text replace - "<code>" to "<syntaxhighlight lang="text">")
Line 7: Line 7:
For Git to work properly, it needs to store some personal information for use in email merge requests and in the commit logging.
For Git to work properly, it needs to store some personal information for use in email merge requests and in the commit logging.


<code>
<syntaxhighlight lang="text">
git config --global user.name "Your Name"
git config --global user.name "Your Name"
git config --global user.email [email protected]
git config --global user.email [email protected]
Line 14: Line 14:
Additionally, to allow colorized output in Git:
Additionally, to allow colorized output in Git:


<code>
<syntaxhighlight lang="text">
git config --global color.ui true
git config --global color.ui true
</code>
</code>
Line 22: Line 22:
If you experience problems with colorized output, try adding the following to your <tt>~/.bashrc</tt>
If you experience problems with colorized output, try adding the following to your <tt>~/.bashrc</tt>


<code>
<syntaxhighlight lang="text">
# R needed for git colours
# R needed for git colours
export LESS="-RIM"
export LESS="-RIM"
Line 31: Line 31:
Getting started with Git for KDE is fairly easy. First, run the following:
Getting started with Git for KDE is fairly easy. First, run the following:


<code>
<syntaxhighlight lang="text">
git clone <REPOSITORY URL>
git clone <REPOSITORY URL>
</code>
</code>
Line 41: Line 41:
Create a work branch (in this example, called <tt>mywork</tt>), and change to that branch:
Create a work branch (in this example, called <tt>mywork</tt>), and change to that branch:


<code>
<syntaxhighlight lang="text">
git branch mywork
git branch mywork
git checkout mywork
git checkout mywork
Line 48: Line 48:
Now change into the git directory and code away. To fetch upstream changes into your local repository, switch to the master branch and run:
Now change into the git directory and code away. To fetch upstream changes into your local repository, switch to the master branch and run:


<code>
<syntaxhighlight lang="text">
git checkout master
git checkout master
git pull --rebase
git pull --rebase
Line 55: Line 55:
To update another branch from master, without losing changes already committed in the branch:
To update another branch from master, without losing changes already committed in the branch:


<code>
<syntaxhighlight lang="text">
git checkout mywork
git checkout mywork
git merge master
git merge master
Line 62: Line 62:
To check the status of the repository. use the <tt>status</tt> sub-command to view the state of the local copy:
To check the status of the repository. use the <tt>status</tt> sub-command to view the state of the local copy:


<code>
<syntaxhighlight lang="text">
$ git status
$ git status
# On branch master
# On branch master
Line 77: Line 77:
If 'testfile' should be added to the local copy, run the <tt>add</tt> sub command:
If 'testfile' should be added to the local copy, run the <tt>add</tt> sub command:


<code>
<syntaxhighlight lang="text">
$ git add testfile
$ git add testfile
</code>
</code>
Line 83: Line 83:
Once 'testfile' has all the changes desired, <tt>commit</tt> it to the local copy:
Once 'testfile' has all the changes desired, <tt>commit</tt> it to the local copy:


<code>
<syntaxhighlight lang="text">
$ 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
Line 92: Line 92:
Much like <tt>svn status</tt>, if you change a file once it has been committed, running <tt>git status</tt> will show that the file was modifed since last commit:
Much like <tt>svn status</tt>, if you change a file once it has been committed, running <tt>git status</tt> will show that the file was modifed since last commit:


<code>
<syntaxhighlight lang="text">
$ echo "new content" > testfile
$ echo "new content" > testfile
$ git status
$ git status
Line 110: Line 110:
To push your changes in the current branch to the upstream repository:
To push your changes in the current branch to the upstream repository:


<code>git push</code>
<syntaxhighlight lang="text">git push</code>


== Branches and merging are cheap in Git ==
== Branches and merging are cheap in Git ==
Line 118: Line 118:
To view the current set of branches available, run <tt>git branch</tt>. The branch with a proceeding '*' is the active one. To create a new branch called <tt>"bugfix-branch"</tt> for example, run:
To view the current set of branches available, run <tt>git branch</tt>. The branch with a proceeding '*' is the active one. To create a new branch called <tt>"bugfix-branch"</tt> for example, run:


<code>
<syntaxhighlight lang="text">
$ git branch bugfix-branch
$ git branch bugfix-branch
$ git checkout bugfix-branch
$ git checkout bugfix-branch
Line 125: Line 125:


There is also an alias for that
There is also an alias for that
<code>
<syntaxhighlight lang="text">
$ git checkout -b bugfix-branch
$ git checkout -b bugfix-branch
Switched to a new branch "bugfix-branch"
Switched to a new branch "bugfix-branch"
Line 132: Line 132:
Make the necessary changes to the branch and then get ready to merge them back to the master branch
Make the necessary changes to the branch and then get ready to merge them back to the master branch


<code>
<syntaxhighlight lang="text">
$ git checkout master
$ git checkout master
Switched to branch "master"
Switched to branch "master"
Line 155: Line 155:
To view the change log for a given file in the local copy, run <tt>git log <FILENAME></tt>.
To view the change log for a given file in the local copy, run <tt>git log <FILENAME></tt>.


<code>
<syntaxhighlight lang="text">
$ git log testfile
$ git log testfile
commit 14a9802e249413003d1fa40002baa025aa54c75f
commit 14a9802e249413003d1fa40002baa025aa54c75f

Revision as of 20:44, 29 June 2011

Warning
This page is yet to be reviewed for changes required by the migration to Git. Information and commands on this page may no longer be valid and should be used with care. Please see the KDE Git hub page for more details.


This tutorial will show you the basics for Git.

Setting up Git

For Git to work properly, it needs to store some personal information for use in email merge requests and in the commit logging.

<syntaxhighlight lang="text"> git config --global user.name "Your Name" git config --global user.email [email protected]

Additionally, to allow colorized output in Git:

<syntaxhighlight lang="text"> git config --global color.ui true

There are other additional color-related features which are discussed in the fine documentation that comes with Git.

If you experience problems with colorized output, try adding the following to your ~/.bashrc

<syntaxhighlight lang="text">

  1. R needed for git colours

export LESS="-RIM"

First steps with Git

Getting started with Git for KDE is fairly easy. First, run the following:

<syntaxhighlight lang="text"> git clone <REPOSITORY URL>

The <REPOSITORY URL> for the KDE-Qt repository is discussed on the Qt Prerequisites wiki page.

This creates a local copy of the upstream repository, in your local master branch. Always keep the master branch for tracking the upstream repository, and create other branches to do your work in. That way, it will be straightforward to push selected changes upstream, rather than having to push all your changes every time.

Create a work branch (in this example, called mywork), and change to that branch:

<syntaxhighlight lang="text"> git branch mywork git checkout mywork

Now change into the git directory and code away. To fetch upstream changes into your local repository, switch to the master branch and run:

<syntaxhighlight lang="text"> git checkout master git pull --rebase

To update another branch from master, without losing changes already committed in the branch:

<syntaxhighlight lang="text"> git checkout mywork git merge master

To check the status of the repository. use the status sub-command to view the state of the local copy:

<syntaxhighlight lang="text"> $ 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)

If 'testfile' should be added to the local copy, run the add sub command:

<syntaxhighlight lang="text"> $ git add testfile

Once 'testfile' has all the changes desired, commit it to the local copy:

<syntaxhighlight lang="text"> $ 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

Much like svn status, if you change a file once it has been committed, running git status will show that the file was modifed since last commit:

<syntaxhighlight lang="text"> $ 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")

As the output states, the changes will not be added to the commit before you use git add to add the files to your staging area. The staging area is a temporary location for the changes to be commited, and it basically allows you to select what files you want to include in a commit. If you want to include all changed files to your next commit, the shortcut "-a" for commit can be used to achieve this.

After the files have been added to the staging area, git diff will not show changes to them. To check your changes before doing the commit, you can pass --cached as an option to diff to see them.

To push your changes in the current branch to the upstream repository:

<syntaxhighlight lang="text">git push

Branches and merging are cheap in Git

When creating changes to the code from the upstream repository that will later get merged back upstream, branches are highly recommended.

To view the current set of branches available, run git branch. The branch with a proceeding '*' is the active one. To create a new branch called "bugfix-branch" for example, run:

<syntaxhighlight lang="text"> $ git branch bugfix-branch $ git checkout bugfix-branch Switched to branch "bugfix-branch"

There is also an alias for that <syntaxhighlight lang="text"> $ git checkout -b bugfix-branch Switched to a new branch "bugfix-branch"

Make the necessary changes to the branch and then get ready to merge them back to the master branch

<syntaxhighlight lang="text"> $ 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 there had been changes made to the branch, git would have automatically tried to merge the changes in the bugfix-branch with the master branch.

Rarely, this can cause a merge conflict. In those cases, manually edit the files that conflict in the master branch, and then do a git commit -a to complete the merge. Git indicates the conflicting lines within the file that cannot be auto-merged.

Viewing the Change log

To view the change log for a given file in the local copy, run git log <FILENAME>.

<syntaxhighlight lang="text"> $ 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

To ease the workflow you can use git log -p to see the diffs per commit basis, instead of querying the commit hash with git log and then using git diff against that.