Jump to content

Git: Difference between revisions

From HW wiki
No edit summary
No edit summary
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
[http://git.or.cz/ Git homepage] - look there for manuals, tutorials and other interesting things or read on bellow.
[http://git.or.cz/ Git homepage] - look there for manuals, tutorials and other interesting things or read on bellow.
== Other docs ==
There is plenty of documentation which is probably better than this page.
[[Image:Git-cheat-sheet-medium.png|thumb]]
* [http://git-scm.com/ Git Homepage] with lots of useful information
* [http://git.or.cz/course/svn.html Git for SVN users]
* [http://www.kernel.org/pub/software/scm/git/docs/gittutorial.html Git for everyone]
* [http://www.kernel.org/pub/software/scm/git/docs/user-manual.html Git User's Manual]
* [http://gitcasts.com/ Git video tutorials]
* http://toolmantim.com/article/2007/12/5/setting_up_a_new_remote_git_repository
* http://eagain.net/blog/2007/03/22/howto-host-git.html
* http://lwn.net/Articles/210045/
* http://git.or.cz/gitwiki/Gitweb


== Install Git ==
== Install Git ==
Line 7: Line 22:
* NetBSD: <pre>cd /usr/pkgsrc/devel/scmgit && make install clean</pre>
* NetBSD: <pre>cd /usr/pkgsrc/devel/scmgit && make install clean</pre>


=== Correct Git version ===
For submodule support (used by sysless framework) you need at least version 1.5.3.4. Check your version by
git --version


Links for newer version for various distributions:
* Ubuntu: https://edge.launchpad.net/~smartlounge/+archive


== Graphical Frontends ==
== Graphical Frontends ==
Line 82: Line 92:
  $ git config --global color.status auto
  $ git config --global color.status auto


* You can change it for individual repositories either without <tt>--global</tt> or in <pre>.git/config</tt>:
* You can change it for individual repositories either without <tt>--global</tt> or in <pre>.git/config</pre>:


  [user]
  [user]
Line 93: Line 103:


== Advanced commands ==
== Advanced commands ==
* Add, move or remove any files
* Add, move or remove any files


Line 131: Line 142:
  $ git checkout -f
  $ git checkout -f


== Branches ==
== Working with multiple remote branches of the same repository ==
* List all branches


  $ git branch
Add remote for a branch
git remote add cartech sojka@rtime.felk.cvut.cz:/var/git/sysless-lpc21xx-cartech.git
Setup a local branch to tract the remote branch
  git branch --track cartech cartech/master


* Checkout a desired branch
Merge all changes between branches
 
  git checkout cartech    # switch to cartech branch
$ git checkout name_of_branch
  git merge master        # merge all changes from master branch to cartech
 
  git push                # push the merged changes to cartech
* Create a new branch and make it current
  git checkout master     # switch back to master
 
$ git checkout -b new_branch master
 
* Merge changes from one branch into another. Let us suppose that you do work on branch A and branch B,
and after work on those two branches is complete, you merge the work into mainline branch M.
 
  $ git checkout M # switch to branch M
  $ git pull . A # merge A into M
$ git pull . B # merge B into M
 
* Delete a branch
 
  $ git branch -d branch_name
 
* Switch to a commit 76ff365f7799e43883d214d7f66a48c5c595c7d2 from the master branch
 
  $ git log
 
<pre>
commit 228ffa28d6b63c3baa809b5b6edf3b5e28336dc2
Author: Username <username@example.com>
Date:  Mon Dec 31 09:29:19 2007 +0100
 
     binary


commit 76ff365f7799e43883d214d7f66a48c5c595c7d2
To merge only some changes use
Author: Username <username@example.com>
  git cherry-pick
Date:  Mon Dec 31 09:21:14 2007 +0100
 
    Initial commit of the Helloworld.
</pre>
 
$ git branch test1 76ff365f7799e43883d214d7f66a48c5c595c7d2
$ git branch
  * master
  test1
$ git checkout test1
$ git branch
  master
  * test1
$ git log
 
<pre>
commit 76ff365f7799e43883d214d7f66a48c5c595c7d2
Author: Username <username@example.com>
Date:  Mon Dec 31 09:21:14 2007 +0100
 
    Initial commit of the Helloworld.
</pre>
 
* Switch back to the master branch and delete the ''test1'' branch
$ git checkout master
  $ git branch -d test1


== Setting up a new project and a new remote git repository ==
== Setting up a new project and a new remote git repository ==
Line 246: Line 208:
  git push                # push the merged changes to cartech
  git push                # push the merged changes to cartech
  git checkout master    # switch back to master
  git checkout master    # switch back to master
== Installation of the web interface ==


== Other docs ==
[[Category:Git]]
* [http://git.or.cz/course/svn.html Git for SVN users]
* [http://www.kernel.org/pub/software/scm/git/docs/tutorial.html Git for everyone]
* [http://www.kernel.org/pub/software/scm/git/docs/user-manual.html Git User's Manual]
* http://linux.yyz.us/git-howto.html
* http://toolmantim.com/article/2007/12/5/setting_up_a_new_remote_git_repository
* http://eagain.net/blog/2007/03/22/howto-host-git.html
* http://lwn.net/Articles/210045/
* http://git.or.cz/gitwiki/Gitweb

Latest revision as of 12:27, 20 October 2010

Git homepage - look there for manuals, tutorials and other interesting things or read on bellow.

Other docs

There is plenty of documentation which is probably better than this page.


Install Git

  • In Debian (Ubuntu):
    apt-get install git-core
  • Git for Windows (MSys) - works quite well. There are other possibilities such as git for Cygwin.
  • FreeBSD:
    cd /usr/ports/devel/git/ && make install clean
  • NetBSD:
    cd /usr/pkgsrc/devel/scmgit && make install clean


Graphical Frontends

  • qgit
  • git gui
  • gitk
  • tig

Basic commands

  • Initialize a brand new Git repository
$ cd /home/project
$ git init         # with git older than v1.5, use init-db instead of init
$ git add .
$ git commit
  • First checkout
$ git clone /home/project
$ git clone username@someserver.org:/path/to/repo.git
  • MinGW port check out project
git clone ssh://username@someserver.org/path/to/repo.git
  • Browse repository and history using a graphical interface
$ qgit

To be able to do commits from QGIT, check View->Check working dir

  • Usefull commands
$ git log
$ git log --stat --color
$ git <whatever> --help
$ man git-<whatever>
  • Before commit
$ git status
$ git diff [--color]
$ git add <file>
$ git commit [-m "message"]
  • To commit all modified files without the need of
    git add
$ git commit -a [-m "message"]
  • Send your commits to the central repository
$ git push
  • Update from server
$ git pull
  • Pull from others
$ git pull name@1.2.3.4:/path/to/repo.git

Configuration

  • Global configureation
$ git config --global user.name "Your Name Comes Here"
$ git config --global user.email you@yourdomain.example.com
$ git config --global color.diff auto
$ git config --global color.status auto
$ git config --global color.status auto
  • You can change it for individual repositories either without --global or in
    .git/config
    :
[user]
       name = "Your Name"
       email = "username@example.com"
[color]
       diff = auto
       status = auto
       branch = auto

Advanced commands

  • Add, move or remove any files
* git add file
* git mv file
* git rm file
  • Shows the commits which add or remove any file data matching string
$ git log -Sstring
  • Show what revision and author last modified each line of a file
$ git blame file
  • Display changes since the last git-update-index:
$ git diff
  • Display changes since the last commit:
$ git diff HEAD
  • List all changesets belonging to a specific file
$ git-whatchanged file
  • Delete all untracked files
$ git clean
  • Restore a file from the last revision
$ git checkout /file/you/want/to/restore
  • Force to restore all modified files
$ git checkout -f

Working with multiple remote branches of the same repository

Add remote for a branch

git remote add cartech sojka@rtime.felk.cvut.cz:/var/git/sysless-lpc21xx-cartech.git

Setup a local branch to tract the remote branch

git branch --track cartech cartech/master

Merge all changes between branches

git checkout cartech    # switch to cartech branch
git merge master        # merge all changes from master branch to cartech
git push                # push the merged changes to cartech
git checkout master     # switch back to master

To merge only some changes use

git cherry-pick

Setting up a new project and a new remote git repository

  • On the remote host
$ mkdir /var/git/project
$ cd /var/git/project
$ git --bare init
  • On your box
$ mkdir /home/project
$ cd /home/project
$ git init                  # with git older than v1.5, use init-db instead of init
$ git add .
$ git commit -a
  • Put the following lines to your /home/project/.git/config
[remote "origin"]
       url = username@example.com:/var/git/project
       fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
       remote = origin
       merge = refs/heads/master
  • Push your project onto the host
$ cd /home/project
$ git push origin master
  • Test if your project has been pushed to the remote host correctly
$ cd /tmp
$ git clone username@example.com:/var/git/project
  • For more details follow the tutorial
http://toolmantim.com/article/2007/12/5/setting_up_a_new_remote_git_repository

Working with multiple remote branches of the same repository

Add remote for a branch

git remote add cartech sojka@rtime.felk.cvut.cz:/var/git/sysless-lpc21xx-cartech.git

Setup a local branch to tract the remote branch

git branch --track cartech cartech/master

Merge all changes between branches

git checkout cartech    # switch to cartech branch
git merge master        # merge all changes from master branch to cartech
git push                # push the merged changes to cartech
git checkout master     # switch back to master