Files
knowledge-kit/Chapter7 - Geek Talk/7.5.md
2020-02-25 17:46:51 +08:00

70 lines
1.8 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Git 实用操作
## 合并多次提交记录
有的时候我们对于某个功能为了实时保存自己写的代码可能会有多次提交所以等功能稳定下来我们可能会有这种需求将前面多余几次的提交记录合并为1个记录。幸运的是 Git 为我们提供了这样的命令。
有2种做法
- 合并部分
- git rebase -I HEAD~n。这里的 n 代表压缩最后n次提交。执行这条命令后会弹出 vim 编辑窗口,这 n 次提交记录会倒序,最上面的是最早的提交,最下面的是最新的提交。
```
pick cc77998 ...
pick 1821f6a ...
...
pick 124422 ...
```
我们需要修改第2到n行的 pick 为 squash这个的意思为将最后n-1次的提交合并为1次提交。然后我们保存退出git 会一个个压缩提交历史,如果有冲突则解决冲突就好。
- 完成之后我们将本地的修改提交到远端。 Git push -f
- 全部合并
```
git rebase -i --root
```
将全部的提交记录合并为1个
## 删除项目中所有的提交记录
之前个人在 Github 开源了一些完整的项目,但是有些人用于商业用途,所以有了这个需求,就是将项目改动一下,删除一些重要代码提交最新的代码到仓库并且让用户不能通过提交的历史记录会滚到指定的版本看到代码。
以下为步骤
```
1.Checkout
git checkout --orphan latest_branch
2. Add all the files
git add -A
3. Commit the changes
git commit -am "commit message"
4. Delete the branch
git branch -D master
5.Rename the current branch to master
git branch -m master
6.Finally, force update your repository
git push -f origin master
```
## 给项目打 tag
```shell
git tag -a 1.0.0 -m 'release SPM lib'
```