个人小站

[Git]一些命令

字数统计: 538阅读时长: 2 min
2020/04/21

如无特别说明,本人的所有Git操作都是以Github为主

git 文档
https://git-scm.com/docs

本地分支删除

查看项目的分支们(包括本地和远程)

1
$ git branch -a  

删除本地分支

1
$ git branch -d <BranchName>

reset 删除错误提交的commit

使用这个的前提条件是没有push,一般不建议push -f
git reset --hard <commit_id> 回退到某一版本。
git reset --hard head~1 回退一个版本(数字2自然就是两个版本啦)
然后 git push --force

rebase 伪造历史

git rebase -i HEAD~10 (修改最近10个历史)

下面是rebase打开的文本里命令的提示(git是通过判断进程创建以及进程是否仍然存在来判断用户是否开始、结束编辑,所以多标签的文本编辑器会影响git判断你是否还在编辑)

1
2
3
4
5
6
7
8
9
10
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]

其中每个历史里的变动占一行,我们只要改前面的pick即可,其中主用的一般是squash,squash会合并改动,再次rebase时是不会拆分的。edit则是修订历史变动里的错误。也可以改动这些变动的顺序。

需要注意的是git里以记录改动为主,如果修改历史变动是会改变现有代码的(特别是你把某个文件的最后一次commit给弄没的时候)。drop 意味着当时进行的修改也会被取消。很容易造成冲突(conflict),
所以一般是建议squash来合并commit

最后 git push --force
这个命令会把远程的彻底抹掉,出问题也会很彻底的,所以要谨慎。

conflict处理

原文作者:ted423

原文链接:http://ted423.github.io/Code/Git/git/

发表日期:April 21st 2020, 2:36:00 pm

更新日期:April 21st 2020, 2:36:00 pm

版权声明:本站原创内容(一般是语句不通顺的那种)采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可,转载内容以及不带个人观点的分享不在此例,摘抄有Wiki的内容的文章统一根据Wiki采用 CC BY-SA 3.0

CATALOG
  1. 1. 本地分支删除
  2. 2. reset 删除错误提交的commit
  3. 3. rebase 伪造历史