Git

From HW wiki
Jump to navigation Jump to search

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.

Git-cheat-sheet-medium.png


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