• remote: remote location of repository
  • origin: default name of the repository
  • master: main branch of repository

Project setup

git init
git add
git commit -m "first commit"
git remote add origin [git_url]
git push -u origin master

change url

git remote -v
git remote set-url origin [git_url]

Get latest commit ID

git log --format=%h -1

Find who modified this line (git blame)

# blame at line 40 of file foo
git blame -L 40,+1 foo

# blame at line 40 - 60 of file foo
git blame -L 40,+21 foo
git blame -L 40,60 foo


git tag -a v2.1.0 -m "xyz feature is released in this tag." # Annotated Tags
git tag v2.1.0 # Lightweight Tags

git push origin --tags # Push tags

git push --delete origin TAGNAME # Delete the tag from the remote repository
git tag -d TAGNAME # Delete the tag locally

# Delete all local tags and get the list of remote tags
git tag -l | xargs git tag -d
git fetch

# Remove all remote tags
git tag -l | xargs -n 1 git push --delete origin

# Clean up local tags
git tag -l | xargs git tag -d

(Clone a specific git branch)[]

git clone -b <branch> <remote_repo>
git clone -b my-branch 

# to prevent fetching of all branches at Git 1.7.10
git clone -b opencv-2.4 --single-branch

Move repo1 to repo2 and keep the history

cd repo2
git checkout master
git remote add r1remote **url-of-repo1**
git fetch r1remote
git merge r1remote/master --allow-unrelated-histories
git remote rm r1remote



# convert git: to http:
git config --global url. git://

# convert http: to git:
git config --global

InsteadOf in configu

[url ""]
  insteadOf =

Fork repository

# Configuring a remote for a fork
git remote add upstream

# View remote
git remote -v

# sync
git fetch upstream
git merge upstream/master

Source Tree

Install source tree command line (if failed installed from the ui)

alias stree='/Applications/'

Enable force push

  • Version 2.7.6: setting > advance

Good Git Commit

  • Clear commit message
    • e.g. “Fix bug” (which bug?)
  • Tell what the changes and why.
    • e.g. “Modify some_file.go” (What changes? Why?)
  • Not too big (step by step as the thinking process)
  • Not too small (focus on important change)
    • “Fix typo” (Too small)
  • Compile and test success
  • Self-testing code
  • Include the required file e.g. migration file
  • No dead/commented code
  • Include pairing partner (using “Coauthored By”)