Skip to content
0

Git

组织多重身份

作为程序员,可能会遇到这个问题,开发公司的项目,需要用自己在公司的用户名和邮箱。开发个人的项目,需要切换到个人的用户名和邮箱

可以在 .gitignore 中用 includeIf 语法匹配路径,当路径匹配的时候,会自动切换到对应的配置

[user]
    name = xxx
    email = xxx@xxx.com

[includeIf "gitdir:~/personal/i/"]
    path = ~/.gitconfig-personal

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work
[user]
    name = xxx2
    email = xxx2@xxx.com
[user]
    name = xxx3
    email = xxx3@xxx.com

modify commit message

将会同当前暂存区的文件一同提交

$ git commit --amend -m 'xxx'

相当于

$ git reset --soft HEAD^
$ git commit -m 'xxx'

如果一直使用如上命令,就可以永远保持一条 commit,虽然是变化的

压缩多个 commit

$ git rebase -i f452a3c # 要合并的前一个 commitId

得到当前距 f452a3c 的多个 commit 记录

pick d94e78 Prepare the workbench for feature Z     --- older commit
pick 4e9baa Cool implementation 
pick afb581 Fix this and that  
pick 643d0e Code cleanup
pick 87871a I'm ready! 
pick 0c3317 Whoops, not yet... 
pick 871adf OK, feature Z is fully implemented      --- newer commit

将除了第一个之外的 pick 都改为 s(squash)

pick d94e78 Prepare the workbench for feature Z     --- older commit
- pick 4e9baa Cool implementation 
+ s 4e9baa Cool implementation 
- pick afb581 Fix this and that  
+ s afb581 Fix this and that  
- pick 643d0e Code cleanup
+ s 643d0e Code cleanup
- pick 87871a I'm ready! 
+ s 87871a I'm ready! 
- pick 0c3317 Whoops, not yet... 
+ s 0c3317 Whoops, not yet... 
- pick 871adf OK, feature Z is fully implemented      --- newer commit
+ s 871adf OK, feature Z is fully implemented      --- newer commit

保存退出 wq,然后写个新的 commit 信息:

new commit message

再保存退出 wq,修改成功:

[detached HEAD 0deaaa5] new commit message
 Date: Sun Nov 12 23:02:17 2023 +0800
 1 file changed, 9 insertions(+)
Successfully rebased and updated refs/heads/main.

分支覆盖

强制让远程的分支覆盖本地的分支

git fetch --all # 拉取
git reset --hard origin v3/test # 覆盖

强制让本地的分支覆盖本地的另一个分支

git checkout -B v3/test

Released under the MIT License.