Git 基本操作
创建Git仓库
查看代码情况
1 2
| git status git diff {filepath}
|
提交代码
1 2 3 4
| git clone {clone.git} git add -A git commit -m "message" git push orgin master
|
保存/恢复临时修改文件
1 2
| git stash git checkout stash@{0} -- {filepath}
|
读取文件历史版本
1
| git checkout {commithash} -- {filepath}
|
创建标签
1 2 3
| git tag -a v1.0 git tag -a v0.9 85fc7e7 git tag
|
Pull/Fork工作流
- fork from the github
- clone to local and commit changes
1 2 3 4 5 6
| git clone {clone.git} git checkout -b feature
git add -p git commit -m "feature message" git push origin feature
|
- rebase commits after remote master
1 2 3 4 5 6 7
| git remote add upstream {remote.git} git pull upstream master
git checkout feature git pull --rebase origin master
git push origin feature --force
|
- raise pull request for code review
提交历史管理
缩减冗余commit log
1 2 3
| git rebase -i HEAD~10 feature git push orign feature --force
|
- 通过squash命令压缩commit history
1 2 3 4 5 6 7 8
| git reset --hard HEAD~12
git merge --squash HEAD@{1}
git commmit -m "xxx"
git push origin master --force
|
分支管理
创建新分支
在创建新的local分支时,也可以添加commit hash告诉git分支最新的HEAD指向
1 2
| git checkout -b feature
|
切换分支/从Detached HEAD切换
删除本地分支
1 2
| git branch -d feature git branch -D feature
|
删除远程分支
1
| git push orgin --delete feature
|
合并分支
CherryPick别的分支commit/branch
在需要apply的分支上保证没有unstaged change,运行如下命令
1
| git cherry-pick <commitHash>/<feature branch name>
|