This post is to archive some commonly used git commands.

Clone

1
2
# Just clone a certain branch from repo and name the local directory as dir
$ git clone -b <branch> <repo> [dir]

Merge

1
2
3
4
5
6
7
8
9
10
11
12
# Go to branch master
$ git checkout master

# Merge branch dev into master
$ git merge dev
# Then handle potential conflicts

# Merge branch dev into master. If conflicted, use master
$ git merge -X ours dev

# Merge branch dev into master. If conflicted, use dev
$ git merge -X theirs dev

Commit

1
2
# Modify the last commit message
$ git commit --amend

Rebase

1
2
3
# Merge the most recent 2 commits
$ git rebase -i HEAD~2
# Then squash the second and following commits and edit the combined commit message

Checkout

1
2
3
4
5
6
7
8
9
10
11
# Create and checkout a new branch (with current change).
$ git checkout -b <branch>

# Create a new local branch tracking origin/<remote-branch>
$ git checkout -b <local-branch> origin/<remote-branch>

# Clear the changes of a file
$ git checkout -- <file>

# Checkout an existing branch
$ git checkout <branch>

Branch

1
2
3
4
5
6
7
8
9
10
11
# List all local branchs
$ git branch

# Create a new branch
$ git branch <branch>

# Delete a branch
$ git branch -D <branch>

# Delete a fully merged branch
$ git branch -d <branch>

Push

1
2
3
4
5
# Forcefully push
$ git push -f

# Push this branch to origin and as remote-branch
$ git push --set-upstream origin <remote-branch>

Reset

1
2
# Discard all uncommitted changes
$ git reset --hard

Clean

1
2
# Remove all untracked files
$ git clean -fd

Submodule

If you need to put a git repo inside another git repo, treat the inside repo as a git submodule.

1
$ git submodule add [OPTIONS] <repo> <path>