Git

From XennisWiki
Jump to: navigation, search

Git is a version control system that is used for software development and other version control tasks. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows. Git was created by Linus Torvalds in 2005 for development of the Linux kernel, with other kernel developers contributing to its initial development. (Wikipedia)

Download and installation

Arch Linux

pacman -S git

Debian

apt-get install git

To get the newest version on older systems

apt-add-repository ppa:git-core/ppa
apt-get update
apt-get install git

Download

Download Git form the official site. On the side you will also find a selection of GUI Clients.

IDE-Integration

Eclipse

In general Eclipse comes with a build in Git support, but further information for especially GitHb you find on the side Git in Eclipse (Plugins EGit - Git Team Provider and GitHub Mylyn Connector are necessary).

Repository browser

gitk
  • Debian: apt-get install gitk

Commands

Basics

Display status

git status

Show commit log

git log

Rename file

git mv file.txt newFolder/newName.txt

Clone and commit

Clone a project

git clone [-b BRANCH-NAME] git@... [DESTINATION-FOLDER]

Add (-a) changes and commit these change with message (-m <text>)

git commit -a -m "Comment"

Tag

Create a tag and push it

git tag <tag-name>
git push origin <tag-name>

Delete a tag

git tag -d <tag-name>

Reset and revert changes

Reset local uncommitted changes (checkout, reset)

Display changes

git diff [--cached]

Reset (1) a single file or (2) all

git checkout path/example.txt
git checkout -- .

Reset the local uncommitted changes

git reset --hard

Revert merge commit

Exemplary merge commit, which you want to revert

commit  6a7cdb9f2d3c6d614f26e20e83cacc2707b37527
Merge: 95db9ea0 7c6b236
...
Merge branch 'master'
...

Use git revert with the option -m <parent-number> (merge). (1) Revert to commit 95db9ea0 (see message above) by using -m 1 or (2) revert to commit 7c6b236 by -m 2

git revert 6a7cdb9 -m 1
git revert 6a7cdb9 -m 2

Branch

Create and push a branch

(1) Create a branch and (2) push it

git checkout -b BRANCH-NAME
git push -u origin BRANCH-NAME

Delete a branch

(1) Delete a local branch and (2) delete a remote branch

git branch -d BRANCH-NAME
git push origin :OLD-NAME

Checkout branch and list all branches

Switch to branch

git checkout BRANCH-NAME

Display all branches

git branch -a

Submodule

Init and update submodules (use --depth 1 for a shallow clone)

git submodule update --init [--recursive]

Checkout each submodule

git submodule foreach git checkout

Add a new submodule

git submodule add git@example.org:my-repo path/to/dir

Reset all submodules

git submodule foreach --recursive git submodule update --init

Miscellaneous

Delete all untracked files

List files (with option -n) to delete

git clean -f -n

Delete all untracked files from the working tree

git clean -f

Tips

see Git - Tips

Show current branch in terminal

Add to your ~/.bashrc the following line

export PS1='\u@$HOSTNAME \w$(__git_ps1 " (%s)")$ '

Bash auto-compleation

Further information: Git Basics - Tips and Tricks

Download the auto-completation file and copy it into /etc/bash_completion.d/.

Useful alias

Further information alias

[alias]
    br = branch
    ci = commit -m
    co = checkout
    cp = cherry-pick
    ls = log -5 --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate
    pu = !git pull --rebase && git submodule update
    re = rebase --interactive
    st = status
    amend = commit --amend
    save = !git add -all && git commit -m 'SAVEPOINT'
    wip = !git add -u && git commit -m "WIP"
    wipe = !git add --all && git commit -qm 'WIPE SAVEPOINT' && git reset HEAD~1 --hard

Alias assing the shell

my-review = !sh -c 'git preview $1 $2' -

Push with SSH (on a repo cloned with HTTP)

git remote set-url origin git@example.org:Xennis/example.git

Error and problems

Check Home Directory

Further information: Setting up the Home Directory on Windows

Error message
Check Home Directory

The environmental variable HOME is not set. The following directory will be used for storing
the Git user configuration and as a default repository location: [...].
If this is not correct please set the HOME environment variable and restart Eclipse.
Solution

See also

External links