Git

From Andreida

Deciding which Source Management Tool to use?

Basics

  • a working directory is using a branch (might be master)
  • changes to files are NOT linked to a branch
  • "git add" is NOT linked to a branch, but says, I will later commit these files to my local repository
  • "git commit"+branch IS linked to a branch and adds the changes to the local repository in the given branch
  • "git push" uploads a branch to another repository (perhaps to another computer)
  • "git fetch" gets update/status information about the complete repository
  • "git pull" downlads updates for the current branch from another repository (perhaps from another computer)

Commands

Basic commands

  • get a repository
    • create one
git init
    • get one
git clone git://github.com/schacon/grit.git
  • look at it
git status
  • add a new file
git add <new file>
  • commit changes to your local repository
    • mark for send and send in one command
git commit -a -m 'committing all changed files'
    • do mark and send in two steps
      • mark for the commit
git add <file> 
      • commit
git commit -m 'message'
  • send data to server
    • check, what you would send to the server
git push -n
    • show changes which will be sent to the server
git diff <hash...hash_from-push-n>  
    • send commits to server, if you started the repository with clone
git push
    • delete a branch:
git branch -d <branch>
git push origin :<branch>

Branches

  • new branch
git branch <name> | git checkout -b <name>
  • switch to branch
git checkout <name>
  • merge branch/hotfix to the master
git checkout master
git merge <name> | git merge origin/<name> --no-ff
  • show existing branches (note the * marking the current one)
git branch
  • not merged yet
git branch --no-merge
  • revert a merge which has been done with --no-ff
git revert -m 1 hash_of_merge_commit
  • not pushed yet:
git log origin/master..master
  • list of changes that will be merged into current branch.
git log ..otherbranch
  • diff from common ancestor (merge base) to the head of what will be merged. Note the three dots.
git diff ...otherbranch
  • not pushed yet
git log --branches --not --remotes

Easier handling

gui

Use SourceTree.

After installation, SourceTree is in c:\Users\<username>\AppData\Local\SourceTree\ Create a short cut yourself if there is none.

auto complete

I have currently problems with this auto complete, if you have problems too, just comment it or remove it.


  • get the file
wget https://raw.github.com/git/git/master/contrib/completion/git-completion.bash
  • source it (in your .bashrc)
. git-completion.bash
  • try it (inside a git directory)
git co <tab><tab>


alias, variant 1

Create the alias:

git config --global alias.logline "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

Use the alias:

git logline

Just so you can play around with it, if you want to understand it better:

git log --pretty=oneline
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit



alias, variant 2

put these into your .bashrc

# simple history
alias gitree='git log --pretty=format:"%h - %an, %ar : %s" --graph'
# colors
Green="\[\033[0;32m\]"
Blue="\[\033[0;34m\]"
Yellow="\[\033[01;33m\]"
Color_Off="\[\033[0m\]"
# better histories, last 30, all
alias gitlol='git log --graph --decorate --pretty=format:"%Cgreen%h %Cred%ad%C(Yellow)%d %Cblue%an %Creset%s" --abbrev-commit --date=short -30'
alias gitlola='git log --graph --decorate --pretty=format:"%Cgreen%h %Cred%ad%C(Yellow)%d %Cblue%an %Creset%s" --abbrev-commit --date=short --all'
# prompt shall show info about the git branch
if [ -f /etc/bash_completion.d/git ]; then
   . /etc/bash_completion.d/git
   GIT_PS1_SHOWDIRTYSTATE=true
   PS1=$Color_Off'[\t \u@\h]'$Color_Off' \w'$Blue'$(__git_ps1 " {%s}")'$Color_Off' \$ '$Color_Off
else
   PS1=$Color_Off'[\t \u@\h]'$Color_Off' \w \$ '$Color_Off
fi
# diff via log or diff
alias gitdifflog='function git_log() { git log --graph --decorate --pretty=format:"%Cgreen%h %Cred%ad%C(Yellow)%d %Cblue%an %Creset%s" --abbrev-commit ..$1; };git_log'
alias gitdiff='function git_diff() { git diff --pretty=format:"%Cgreen%h %Cred%ad%C(Yellow)%d %Cblue%an %Creset%s" ...$1; };git_diff'

call:

gitdifflog OtherBranch1
gitdiff OtherBranch1


Tutorials / Manuals

* http://git-scm.com/book

.gitconfig

[color]
  branch = auto
  diff = auto
  status = auto

[color "branch"]
  current = yellow reverse
  local = yellow
  remote = green

[color "diff"]
  meta = black bold
  frag = magenta bold
  old = red bold
  new = green bold

[color "status"]
  added = yellow
  changed = green
  untracked = cyan


special commands

git reset

git reset will move your merge/add/commit actions to a point in history, undo them:

git reset --soft HEAD~1  // committed things are now only added
git reset --mixed HEAD~1 // committed things are not added anymore
git reset --merge        // all changes from "git merge" are removed
git reset --hard         // all changes since last push are removed

remote

show info about the remote repository:

git remote show origin

save user/password

If you are asked for user/password each time you use "git push" and you have no problem having the credentials stored in a not very safe way locally:

git config credential.helper store
git push
git push

The first "git push" will again ask for user/password, the second one should not need it any longer.


change commit message with SourceTree

  • select commit BEFORE the one you want to change
  • right click the selected commit
  • rebase children of ... interactively
  • select the commit you want to change
  • "Edit Message"
  • Ok
  • Ok


If it does not work, first thing to try: just try it again. At least once that worked for me.

Stories

Merge after pull

  • I pull and have a clean status.
  • Git fan who uses git since years does a push.
  • I pull and have to merge
  • Git fan: "The reason you don't understand why you have to merge is, that you don't understand how git works internally."

We don't need no client/server source management in this project

  • git developer working since weeks on a ticket
  • asked where the code is:
  • "we only push when the code is done"