Git使用
修改上一次的commit信息,
1 | git commit --amend -m "some message" |
强推
1 | git push -f |
情况:我在A分支commit了一些修改,我想在B分支做同样的更改:
1 | # 查看 commit hash |
丢弃工作区修改:
未提交到暂存区的
1
2git restore file1 file2
git restore .已经 add 到暂存区的
1
2
3git restore --staged file1 file2
git restore --staged .
# 此时修改从暂存区回退到工作区,如果想丢弃工作区改动,参考上面的命令
git status发现文件名是\数字这种形式,原因是git将非ASCII编码的字符转义了(汉字是utf-8编码),解决办法(当前仓库生效,并不是全局生效):
1 | git config core.quotepath false |
初始化克隆子模块:
1 | git submodule update --init --recursive |
合并多个commit:
1 | # 撤销 commit_sha 之后的提交,保留修改到工作区 |
情况:当前在A分支写代码,需要切换到B分支,直接checkout会报错,而且A中代码还没写完不想commit:
1 | # 暂存修改,此时工作区变动将被暂存,可以切换分支 |
删除已经追踪的文件:
1 | git rm -r --cached <path> |
删除远程分支:
1 | git push origin --delete test_memory_leak_py |
设置分支上游:
1 | git push -u origin <分支名> |
作用:将origin <分支名>设置为当前分支的上游,以直接使用git push
把一个文件改炸了想恢复到head(但是更推荐用restore):
1 | git checkout 文件名 |
git rebase:
原来:
1 | main: A---B---C---D |
你执行:
1 | git checkout feature |
变成:
1 | main: A---B---C---D |
E' F' G' 内容相同,但因为“重新贴上去”,所以 commit hash 变了。
提交历史线性干净,不会产生一个合并提交
想删除一串提交中的某一笔提交:
1 | git log --oneline |
把要删除的那笔提交直接删除,或者将pick改为drop,ctrl+R然后y然后enter,强推到远端,完成。
不小心git add .了。如何撤销:
1 | git restore --staged . |
情况:将一些文件推送到了远端想撤销:
1 | git rm --cached p1_by_rule.json # 从暂存区移除,本地文件保留 |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Moon Light!


