create a local repository

1)set up remote repository

mkdir ~/example.git
cd example.git/
git init --bare

2)push some content into repository

mkdir workspace
cd workspace/
git clone /Users/xyz/example.git
ls  # note example dir is automatically created.
cd example/ 
touch abc.txt
vim ./abc.txt 
git add ./abc.txt 
git commit -a -m "initial abc.txt"
git push -u origin master

3) on another place, you can verify via creating another git client and clone

git clone /Users/xyz/example.git
cat ./example/abc.txt

create remote repository

1) set up initial remote repository with login user xyz on

ssh  # ssh into host with user name xyz.
mkdir my_project.git
cd my_project.git
git init --bare
git update-server-info # If planning to serve via HTTP

2) push content of master branch in remote repository from a local pc.

cd my_project
git init
git touch readme.txt
vim readme.txt
git add readme.txt
git commit -m "My initial commit message"
git remote add origin
git push -u origin master

3) now you can clone the repository from any pc.

git clone
cd my_project

4) suppose you want to create another branch in remote repository from any pc.

mkdir test
cd test
git clone
cd my_project/
git branch hotfix
git checkout hotfix
vim ./readme.txt 
git commit -a -m "update with world"
git push -u origin hotfix

now remote repository has two branches: master and hotfix

5) suppose you want to cherry the changes in step 4 into master
you can continue work from step 2)

git pull
git branch -a # now it shows 3 branches, master, origin/master, origin/hotfix
git cherry-pick commit-id in step 4.


quick commands

mkdir project3
cd project3
git init
git add .
git commit -m "1st commit"

mkdir -p ~/Dropbox/git/project3.git
git init --bare ~/Dropbox/git/project3.git
git remote add origin ~/Dropbox/git/project3.git/
git push origin master

// on another computer
git clone ~/Dropbox/git/project3.git/

approach 1 : set up local repository and clone to dropbox

1. set up local project repository first

cd project1
git init .
git commit -am "Initial Commit"

2. clone your existing local repo into the shared dropbox folder:

we already have ~/Dropbox/git exists.

git clone --bare . ~/Dropbox/git/project1.git

the above command will create the project1.git automatically. The —bare option tells git to not include the project files. Only those files
needed to track the versioning are cloned (mainly those present in the .git/ folder).

git remote add dropbox ~/Dropbox/git/project1.git

3. push content to dropbox

git push dropbox master

4. you (or others) clone from dropbox on other computers

git clone ~/Dropbox/git/project1.git

If they want to have a dropbox remote instead of the default origin:

git remote add dropbox ~/Dropbox/git/project1.git

They would then push the same way:

git push dropbox master

5. you can get updates from others

git pull dropbox master

approach 2: set up dropbox repository and clone to local

1. set up the dropbox repository first

cd ~/Dropbox
mkdir -p git/home.git
cd home.git/
git init --bare

2. map repository into local

cd ~
git clone --no-hardlinks --no-checkout ~/Dropbox/git/home.git/  ~/a.tmp
mv ~/a.tmp/.git ~
git reset HEAD
rm -rf ./a.tmp

3. add home directory files into repository

git add ./.tmux.conf 
git commit -a -m "add .tmux.conf file"
git push origin master

4. pull the same setting files from another computer

repeat the same step 2 and then do a pull

git pull

quick cheatsheet

rebase vs merge

which branch should change, i check out that branch.

feature rebase on master
master merge into feature.

you can rebase the feature branch onto master branch using the following commands:

git checkout feature
git rebase master

Or, you can condense this to a one-liner:

git rebase master feature

But, instead of creating a merge commit on feature, rebasing re-writes the project history by creating brand new commits for each commit in the feature branch.

The easiest option is to merge the master branch into the feature branch using something like the following:

git checkout feature
git merge master

Or, you can condense this to a one-liner:

git merge master feature

This creates a new “merge commit” in the feature branch that ties together the histories of both branches


you can fast-forward the master branch into feature:

$ git checkout master
$ git merge feature
  • compare two branches

The .. syntax is a very useful tool for comparing branches. The next example displays a brief overview of all the commits that are in some-feature branch that are not in master.
git log —oneline master..some-feature

  • git reset

You should never use git reset <commit> when any snapshots after <commit> have been pushed to a public repository. you have to assume that other developers are reliant upon it.

  • Cloning and Creating a Patch

$ git clone git://
$ cd hello-world
$ (edit files)
$ git add (files)
$ git commit -m 'Explain what I changed'
$ git format-patch origin/master
  • Creating and Commiting
$ cd (project-directory)
$ git init
$ (add some files)
$ git add .
$ git commit -m 'Initial commit'


—soft – The staged snapshot and working directory are not altered in any way. useful for quashing commits(though you can use rebase). refer:
—mixed – The staged snapshot is updated to match the specified commit, but the working directory is not affected. This is the default option.
—hard – The staged snapshot and the working directory are both updated to match the specified commit.

a file has been accidentally committed.

git checkout HEAD~ path/to/file
git commit --amend

understanding GIT
GIT magic
Quick tutorial :

rollback to the version in staging area:
  git checkout -- file.txt file2.txt

rollback staging area to the version in commit
  git reset -- file.txt file2.txt

rollback both working and staging area to a version of a commit
  git checkout HEAD file.txt
  git checkout master~2 file.xe
  git reset --hard HEAD 

git diff   file.txt       compares what is in your working directory with what is in your staging area

git diff --staged.   see what you’ve staged that will go into your next commit, it compares your staged changes to your last commit:
git diff --cached file.txt     see what you’ve staged so far (--staged and --cached are synonyms):

git diff file.txt HEAD   working area vs HEAD

git diff sha1 sha2        diff two commits.

If you run git difftool instead of git diff, you can view any of these diffs in software like emerge, vimdiff and many more

git commit --amend -C HEAD   // update last commit

git rm --cache a.txt // remove file stage area but keep local copy in working area
refs and heads



.git/HEAD is symbol link to either of them when you switch branch

2) if there is a master branch on the remote repository you can access the log of that branch locally via

$ git log origin/master
$ git log remotes/origin/master
$ git log refs/remotes/origin/master

They’re all equivalent, because Git expands each of them to refs/remotes/origin/master.

normally. refs/remotes/origin/HEAD points to refs/remotes/origin/master

To pull the master branch on the remote down to a different name as origin/mymaster locally, you can run

$ git fetch origin master:refs/remotes/origin/mymaster

because the default it is : refs/heads/master:refs/remotes/origin/master

to push a branch to remote with a different name:

git push origin master:refs/heads/qa/master

because the default is: git push origin master

git rev-parse —git-dir


if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
   # Initial commit: diff against an empty tree object


files=$(git diff-index --check --cached $against -- | sed '/^[+-]/d' | perl -pe 's/:[0-9]+:.*//' | uniq)
for file in $files ; do
    sed -i 's/[[:space:]]*$//' "$file"
    git add $file

if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
   # Initial commit: diff against an empty tree object


files=$(git diff-index --check --cached $against -- | sed '/^[+-]/d' | perl -pe 's/:[0-9]+:.*//' | uniq)
for file in $files ; do
    diff=$(git diff --cached $file)
    if test "$(git config diff.noprefix)" = "true"; then
    echo "$diff" | patch -R -p$prefix
    diff=$(echo "$diff" | perl -pe 's/[ \t]+$// if m{^\+}')
    out=$(echo "$diff" | patch -p$prefix -f -s -t -o -)
    if [ $? -eq 0 ]; then
        echo "$diff" | patch -p$prefix -f -t -s
    git add $file
sudo cp ~/.git/hooks/pre-commit /usr/share/git-core/templates/hooks/
sudo chmod 755 /usr/share/git-core/templates/hooks/pre-commit
uncommit and recommit

Undo a commit and redo

$ git commit … (1)
$ git reset —soft 'HEAD^' (2)
$ edit (3)
$ git add …. (4)
$ git commit -c ORIG_HEAD (5)

you can use $git show ORIG_HEAD to show details of the commit. ORIG_HEAD is a file inside .git


suppose I fixed something on dev branch and others are working on release branch.
i need to create the patch on dev and against master

git format-patch release —stdout > fix.patch

NOTE, on gitk, to form this patch, the from will be release branch, the to will be dev. (to is the branch has the fix.)

then others on release branch or any other branch that is based on the HEAD of release branch can apply the patch:
check first:
git apply —check fix.patch
git apply —check fix.patch


suppose you have two branches topic and master, and you would like to pick up changes from master.

      A---B---C topic
D---E---F---G master
              A'--B'--C' topic
D---E---F---G master
git rebase --onto <graft-point> <exclude-from> <include-from>

<graft-point> is the new base or the branch to rebase with.
<exclude-form> <include-form> defines the range of elements of (A B C)

If you don’t specify —onto, <graft-point> defaults to <exclude-from>
If you don’t specify an <include-from>, <include-from> defaults to the current branch.

now you can rebase with each approach:
*) on any branch, run git rebase —onto master master topic
*) on branch topic ru: git rebase master

Recommended approach to keep commits of A B C since they lost after the rebase:
1) git branch -b topic-rebased topic
2) git rebase master
NOTE: it is also recommend to add parameter git rebase —preserve-merges

now you can use topic-rebased. the final chart is :

              A'--B'--C' topic-rebased
D---E---F---G master
      A---B---C topic

to abort the rebase:

git rebase --abort

if there is conflict, resolve them and commit, thenresume with

 git rebase --continue

complicated case:

                       H---I---J topicB
              E---F---G  topicA
A---B---C---D--K  master


             H'--I'--J'  topicB
            | E---F---G  topicA
A---B---C---D--K  master

how to identify <exclude-form> <include-form>?
it should define (H, I, J)
thus, the form is git rebase —onto master topicA topicB

rebase to re-organize commit
git rebase -i master~3

Note master~3 commit won't be included in the set of commits(master~2, master~1, HEAD) to be reorganized.

show files in staged area.
git ls-files --stage
compute SHA1 of a file
 git hash-object  file
show a specific version of file
git show master~2:c.txt

notice git cat-file only shows repository object file content in .git directory, but the actual file

git cat-file -p 3b18e512d

look up objects by a unique prefix of the object hash

git rev-parse 3b18e512d

then git will print the long SHA1 : 3b18e512dba79e4c8300dd08aeb37f8e728b8dad

git log
git log --graph --pretty=oneline --abbrev-commit

it shows a nice text graph


with regard to merge, Git computes a merge result and creates a new commit that represents the new unified state.

The git merge operation is context sensitive. Your current branch is always the target branch, and the other branch or branches are merged into the current branch.
for instance, merge dev branch into release branch

git checkout release
git merge dev

local storage:
.git/MERGE_HEAD: the tip of the other branch is temporarily recorded in the symref MERGE_HEAD.
.git/MERGE_MSG: contains the default merge message used when you git commit after resolving the conflicts.

resolve conflicts

list commit-1 on branch-1 vs commit-2 on branch-2 and the conflict.

git log --merge --left-right -p file-x


git diff --ours  == git diff HEAD

because it shows the differences between “our” version and the merged version. Similarly,
git diff --theirs == git diff MERGE_HEAD
git diff --base to see the combined set of changes since the merge base.

If you have botched a conflict resolution and want to return to the original conflict state before trying to resolve it
again, you can use the command

git checkout -m file-x

if you have not commit the merge, you can undo it via:

 git reset --hard HEAD

if you have committed the merge, you can undo as:

git reset --hard ORIG_HEAD
generic linux diff and patch

for file:
diff -uN file new-file > patch.file

for directory:
diff -uNr dir new-dir > patch.dir

-r to recurse through all subfolders of the passed folders
-u to get the “unified format” output
-p prints the c-function a change was contained in
-N treats absent files as empty

apply patch:
patch -p1 < patch.file
patch -p1 < patch.dir

git patch

suppose you create a bugfix branch to fix something, then on the bufgix branch:
(others are working on release branch).

i need to create the patch on bugfix and against release

git checkout bugfix

git format-patch release —stdout > fix.patch
git diff release > fix.patch (press q to exit)

NOTE, on gitk, to form this patch, the from will be release branch, the to will be bugfix . (to is the branch has the fix.)

then others on release branch or any other branch that is based on the HEAD of release branch can apply the patch:
check first:
git apply —check fix.patch
git apply —check fix.patch


to use git patch for patch command:
option-1) if the git patch is generated without special instructions, patch -p1 is required.

$ git diff > save.patch
$ patch -p1 < save.patch


git diff --no-prefix > diff.patch
patch -p0 < diff.patch
git remote push, pull

rebase vs merge

git and repo


gitk —all
git mergetool

update an old change

git stash

git stash save "your message here"
git stash apply -–index stash@{0}
git stash list

basic concepts

Here, master is a branch in the local repository. remotes/origin/master is a branch named master on the remote named origin. You can refer to this as either origin/master, as in:
remotes/origin/head or refs/remotes/origin/master

refs/heads/master The HEAD is "your current branch", or the branch to which Git will commit. S


git clean -f
This will remove all untracked files.
Good basic tutorial
git rebase –I HEAD~3
When you save and exit the editor, Git rewinds you back to the last commit in that list. You can check with: git log —oneline:


git remote very clear explained well explained

It’s important to note that when you do a fetch that brings down new remote branches, you don’t automatically have local, editable copies of them. In other words, in this case, you don’t have a new serverfix branch — you only have an origin/serverfix pointer that you can’t modify.
To merge this work into your current working branch, you can run git merge origin/serverfix. If you want your own serverfix branch that you can work on, you can base it off your remote branch:
$ git checkout -b serverfix origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'

Inspect remotes:

git remote show origin
git remote –v
git remote –r


repo commands:

repo list projects:!topic/chromium-os-dev/xBtwK0RqRws

repo —trace

Check repo —trace rebase . — I just ran this locally on autotest and

a simple work flow

In the source repository, create a patch based on the commit by running
git format-patch SHA1_OF_COMMIT~..SHA1_OF_COMMIT
(note the ~).
This will create a .patch file that describes this commit.

Apply the patch
In the target repository, apply the patch with
git apply PATH_TO_PATCH

If the patch does not apply cleanly, git will not do anything. If you want it to apply as much as possible, use
git apply PATH_TO_PATCH –reject

The parts that could not be applied cleanly end up in .rej files. Take care of those manually.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License