Git

Basics

git init
Initialize new repository in local directory. Nothing commited yet!
git status
Check status of repository
git add <file>
Add <file> to staging area
git add .
Add all modified files to staging area
git commit -m "<comment>"
Commit current staging area with comment
Staging area
Before commiting changes they must be staged. This allows to select only specific changes

History

git log
List what changes have been made
git hist
Custom list (see Aliases)
git log --pretty=oneline
Easier to view format
git log --graph
ASCII graph layout
HEAD
The branch and version to which the working directory points to
detached HEAD
The working directory doesn't point to the latest version of a specific branch anymore. All commits will be lost when switching to a branch if you don't create a new branch for them.

Options

git log --pretty=oneline --max-count=2 git log --pretty=oneline --since='5 minutes ago' git log --pretty=oneline --until='5 minutes ago' git log --pretty=oneline --author=NAME git log --pretty=oneline --all

Old versions

git checkout <hash/tag>
Checkout an old revision
git checkout <tag>^
Checkout the version before an old tag
git checkout master
Return to latest version of the master branch

Tagging

git tag v1
Create the tag v1
git tag
Show all tags
git hist master --all
Show tags in the log

Fixing Mistakes

git checkout <file>
Overwrites any changes in the current working directory for <file>
git reset HEAD <file>
Removes file from staging area. The change will still be in the working directory
git hist master --all
Show tags in the log
git revert HEAD
Undo all changes in the last commit (Creates a new commit)
git revert <hash/tag>
Undo all changes in the specified commit (Creates a new commit)
git reset --hard <hash/tag>
Remove all commits after the specified. They can still be viewed via git hist --all until the garbage collection runs
git tag -d <tag>
Remove a tag
git commit --amend -m "<comment>"
Add a change to the last commit instead of creating a new commit

Branching

git branch <branchname>
Creates a new branch. Note that the working directory isn't automatically switched to the new branch. You still need to checkout the new branch or use the shortcut below
git checkout <branchname>
Switch working directory to a different branch. Use git checkout master to switch back to the master branch
git checkout -b <branchname>
Shortcut for doing git branch <branchname> followed by git checkout <branchname>
git log --graph --all
Display all diverging branches via a ASCII graph
master
master is the default name for the main branch

Merging

git merge <branchname>
Merges <branchname> into the current branch.
git rebase <branchname>
Merges <branchname> into the current branch and integrates all commits into the history of the current branch.
CONFLICT (content): Merge conflict in ...
Conflict during merging. Files got modified in both branches and can't be merged automatically. The different versions got written into the conflicting files. Fix and commit them manually.
Rebase vs. merge
Rebase changes the history. This means information about where the changes came from (i.e. the other branch) will be lost. Recomendation: Use rebase for short-lived local branches to keep the history readable but use merge for everything else - especially when collaborating with other developers
Fast-forward merge
A fast forward merge happens when the branch get created, modified and merged back without the original branch being changed in the meantime. In this case the HEAD of the orginal branch gets set to the HEAD of the modified branch. No further merging necessary and no conflicts possible

Multiple Repositorues

git clone <repo> <cloned_repo>
Creates a copy of the repository.
git clone --bare <repo> <bare_repo.git>
Creates a bare repository (without working files).
git remote
Lists all known remote repositories
git remote show origin
Shows details of the remote repository
git remote add <new name> <remot repo>
Adds a remote repository to the current one
git branch -a
List local and remote branches
git fetch
Copies all changes from the original repository into the cloned one. They do not get merged into local tracking branches. Use git merge origin/master to do that
git pull
Copies all changes and merges them into the current branch
git branch --track <branch> origin/<remote branch>
Adds a new tracking branch
git push <repo> <branch>
Copies all changes from the local branch into the remote repository
origin
Default name for a remote repository.
Fast-forward merge
A fast forward merge happens when the branch get created, modified and merged back without the original branch being changed in the meantime. In this case the HEAD of the orginal branch gets set to the HEAD of the modified branch. No further merging necessary and no conflicts possible
Remote-tracking branch
A remote-tracking branch is a mirror of a branch in the remote repository. It can be identified by the prefix remotes/... git clone automatically creates remote-tracking branches. They are read-only
Tracking branch
A tracking is local branch that is connected to a remote-tracking branch. It can be used tu pull changes fromor push changes to the remote repository. got clone automatically create a tracking branch for remotes/origin/master

Internals

git mv <file> <new_file>
Move file. Equivalant of deleting the old file and adding it under the new name.
git mv <file> <new_file>
Move file. Equivalant of deleting the old file and adding it under the new name.
git cat-file -t <hash>
Print the type of a git object
git cat-file -p <hash>
Print the content of a git object

Getting Started

Configuration

git config --global user.name "Your Name" git config --global user.email "your_email@whatever.com"

Aliases

Git aliases in file $HOME/.gitconfig.

[alias] co = checkout ci = commit st = status br = branch hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short type = cat-file -t dump = cat-file -p

Shell aliases in file $HOME/.profile.

alias gs='git status ' alias ga='git add ' alias gb='git branch ' alias gc='git commit' alias gd='git diff' alias go='git checkout ' alias gk='gitk --all' alias gx='gitx --all' alias got='git ' alias get='git '

Links

http://gitimmersion.com