I will try to keep this post updated with useful tips for git usage.

Currently the content is inspired by the excellent book I read recently Pragmatic Version Control Using Git by Travis Swicegood.

#Git config

git config --global --list
user.name=rk13
user.email=vladimir@kotov.lv
user.signinkey=7F14F979
color.ui=auto

#Pushing project folder to git

Commit names are SHA-1 hashes generated by Git to keep track of a commit.

mkdir mysite
cd mysite
git init
git add file
git commit -m "added X"

#Making changes

There are three places in Git where your code can be stored. First, the one you work with directly when editing files is the working tree. Second is the index, which I’ll refer to as the staging area. The staging area is a buffer between your working tree and what is stored in the repository, the third and final area in Git. You can use the staging area to stage only the changes you want to commit to the repository.

git add changed.file
git status
git commit -m ""
git log -1

The command has the -a parameter on it. That tells Git to commit all files that it knows about that have changed

git commit -a

You can use Git’s interactive add mode to select which files or parts of files to stage for a commit. You start it by adding the -i option.

git add -i

#Branching

git branch RB_1.0 master
git checkout BR1

You use git branch to delete the branch by adding the -d parameter before the branch name

git branch -d RB_1.0

Create branch + checkout with single command.

git checkout -b alternate master

#Rebase

Rebasing takes the changes from a branch and replays them on top of another branch

git checkout master
git rebase BR1

#Changes

Calling git diff with no parameters shows you the changes in your working tree that you haven’t staged or committed yet.

git diff

View the differences in the staging area and the repository by adding –cached to the call:

git diff --cached

The differences in the working tree (also staged) and your repository

git diff HEAD

Change statistics since tag 1.0

git diff --stat 1.0</pre>

#Merging

Straight merging - pulls the entire history of one branch inyo another

git merge alternate_branch</pre>

Squashing commits - all the history of one branch compressed into one commit (f.e. doing bugfix)

git merge --squash emailfix

Cherry-picking commits - if we need only selected commit(s) from branch

git cherry-pick 4f3f42b

#History

git log
git log 5435dgfd
git log --since="5 hours"
git log 80fd599c5499055ba799969c6fdc8b09cc734843..HEAD
git log --pretty=format:"%h %s" 1.0..HEAD - since particular tag
git log --pretty=oneline 1.0..
git log -1 HEAD^^
git log -1 HEAD~3
git blame  README
git blame -L 1,2 README
git blame -L 1,+2 README</pre>

#Undo

git revert -n HEAD
git reset --soft / --hard HEAD^</pre>

#Configure remote

git remote add origin git@git:groovy-study.git
git push origin master
git config --add branch.master.remote origin
git config --add branch.master.merge refs/heads/master</pre>

or edit in .git/config

[branch "master"]
  remote = origin
  merge = refs/heads/master</pre>

#Practices

  • Tag milestones
  • Branch releases
  • Use directory-like structure names

#Useful external links