git
- 配置
1
2
3
4
5
6git config --global user.name "Jeffrey"
git config --global user.email "jeffreyfly@icloud.com"
git config --global pull.rebase true
git config --global credential.helper store
git config --global push.default simple
git config --global -l - 合并提交(本地未push)
1
2
3git status
git add -u
git commit --amend --no-edit - commit 合并
- 方法1: 将你想合并的提交改为 squash 或简写 s,保留第一个 pick 不动:
1
2
3
4
5git rebase -i HEAD~3
pick abc123 Commit message 1
pick def456 Commit message 2
pick 789abc Commit message 3保存退出,Git 会让你编辑最终的 commit message,合并或者修改后保存即可。1
2
3pick abc123 Commit message 1
squash def456 Commit message 2
squash 789abc Commit message 3
强行推送:1
git push origin your-branch --force-with-lease
- 方法2
1
2
3
4
5
6
7
8
9# 回到上一次 push 的状态(或者想作为基准的 commit)
git reset --soft origin/your-branch
# 或者 git reset --soft HEAD~N (如果你想数 N 个提交的话)
# 将所有改动重新提交为一个 commit
git commit -m "合并后的 commit message"
# 推送到远程(已 push 的分支需要强制)
git push --force-with-lease
忽略所有名为 build 的目录
编辑.gitignore 文件1
2
3
4
5
6
7
8保留仓库根目录下的 build/README.md
build/
**/build/*.log
//任意层级 build 目录下的 README.md
build/
!**/build/
!**/build/README.mdrebase
1
2
3
4
5
6git fetch origin
git rebase origin/main
git add xxx.cpp
git rebase --continue / abort
git push --force-with-lease
git push orign <localbranch>:<remotebranch>把文件回退到它的上一次修改
1
git checkout $(git log -n 1 --pretty=format:%H -- path/to/file) -- path/to/file
只查看发生变更的文件名(不看具体 diff 内容)
1
2
3
4git diff --name-only # 工作区 ↔ 暂存区
git diff --staged --name-only # 暂存区 vs HEAD
git diff HEAD --name-only # 工作区 vs HEAD
git diff origin/main HEAD --name-only # 远端分支 vs HEAD更改作者
1
git commit --amend --author="Your Name <your@email.com>"