Common Git Commands

git merge / git rebase

git merge leaves existing commits unchanged, while git rebase appends the feature branch’s changes as patches after the master branch. git merge faithfully preserves commit history, while git rebase makes the history tidier. (A caveat about rebasing: if commits have already been pushed to a public repository, others may have based further work on them, so rebasing those commits is not recommended.)

# git merge的操作流程
## 先切换到主分支的最前端
git checkout master
## merge feature
git merge feature
## 解决冲突
## 添加并提交
git add ./
git commit -m "solve the conficts"
# git rebase的操作流程
## 先切换到feature分支
git checkout feature
## 变基
git rebase master
## 解决冲突
## 提交
git add ./
## 继续变基
git rebase --continue ## 会提示先修改一下上一个feature的提交信息
## 解决冲突
## 提交
git add ./
## 继续变基
## ...
# 进行一次fast-forward将master移动到主分支最前面
git checkout master
git merge feature
## 最后看删除feature分支feature分支指南
git branch -d feature

### 此外还可以通过 git rebase -i 来合并多条提交记录
git rebase -i HEAD~3

Diagram After Merging with git merge

Diagram After Integrating with git rebase

git log / git reflog

git log shows commits, while git reflog shows the entire history of the HEAD pointer, including deleted entries. git reflog only shows local history.

# git log 用法
## 查看所有提交
git log 
## 查看提交的差异
git log -p
## 查看最近两次提交的差异
git log -p -2
## 按一定的形式显示提交历史
git log --pretty=format:"%h - %an, %ar : %s"
## 以ascii字符的形式显示所有的提交历史
git log --oneline --decarate --graph --all
# git reflog 用法
## 按时间查看引用日志
git reflog
## git reflog 一般和git show git reset --hard 配合使用
## 用来恢复已经删除的提交节点

Detailed format parameters

git cherry-pick

Move certain branches to other branches. This is flexible, but it is not best practice: it creates duplicate commits for the same nodes. In most cases, git merge can be used instead.

# 基本命令
git cherry-pick HASH
## 当然之后还是要解决冲突
# 移动多个
git cherry-pick hashA hashB
# 移动连续的多个
git cheery-pick hashA..HashB

git push / git pull

git push pushes a branch to a remote repository, while git pull fetches a branch from the remote repository and merges it into the current branch.

# git push 基本用法
## git push <remote> <remote_branch>:<local_branch>
## 和当前branch合并时:<local_branch>可以省略
## 默认只有通过fast-forward能够merge才能成功。除非加--force选项
git push origin master
git push origin serverfix
## 除此之外,--all 会推送所有分支; --tags 会推送标记 --delete 删除远程分支
# git pull 基本用法
##git pull <remote> <remote_branch>:<local_branch>
## 相当于 git fetch and git merge
# 一般用法
git pull <remote>
## merge时不创建新的commit
git pull --no-commit <remote>
## 采用变基的方式合并
git pull --rebase <remote>
## 输出pull过程中的详细信息
## git pull --verbose