git用法集锦

重要!

一切记录都可以通过如下命令获取,一切操作都可以得益于这个命令找回

# git reflog

 

一:Commit


 

场景1: 修改前一次commit的message信息

# git log
commit 16a60240aa5328fe47b23b00bf752b21fe634bca (HEAD -> add-upload-api, origin/add-upload-api) ---第2次commit
…
commit 60b3e9b90cfb75dfbfcb0f8b82b932ebb56657e5 ---第1次commit,待修改log的commit
Author: root <root@lcmaas-wksp-g.novalocal>
Date: Thu Nov 11 08:45:30 2021 +0000

....some message should be correct....

commit f3da70fe98c083781335e61a35a18c52b91392ac (origin/master, origin/HEAD, master) ---第0次commit,基commit

 

【操作步骤】
1. 重置前两个commit, 将待修改的commit的操作由 "pick" 改成 "edit", 注意这里的顺序和log的是相反的

# git rebase -i HEAD~2

edit 60b3e9b ...some message should be correct.... ---由pick改成edit
pick 16a6024 fix style mistakes detected by golint

# Rebase f3da70f..16a6024 onto f3da70f (2 commands)

保存退出
Ctl + O (Write Out) -> 回车
Ctl + X (Exit)

 

2. 修改message

# git commit --amend

保存退出
Ctl + O (Write Out) -> 回车
Ctl + X (Exit)

 

3. 提交修改

# git rebase --continue
Successfully rebased and updated refs/heads/add-upload-api.

# git push origin xxx (maybe need -f)

 

场景1.5:修改之前某一次commit的message信息

      整体思路: 基于指定commit的前一个commit进行rebase,因为这样会“涉及”这次commit

# git log --oneline
e33ad59  message3
e95f51f    message2    ###待修改commit
2418162  message1

#git rebase -i 2418162a79fe52efea8dd0818f4c47b43583d9b0

...pick -> edit, 保存退出...

# git commit --amend
...修改,保存,退出...

#git rebase --continue

 

场景2:间隔合并commit, 比如想针对之前的某个commit(即不是最近的那个)做一些修改

      整体思路: 将新的修改作为一次commit,然后做rebase,即重置base

      例如如下的情况下,想针对第一次commit再增加一些修改:

# git log --oneline
be6ce3c my second commit --第2次commit
60b3e9b my first commit --第1次commit
f3da70f (origin/master, origin/HEAD, master) Add CONTRIBUTING.md  --第0次,将作为base commit

 

0. 将当前的修改作为一次新的commit提交

# git add
# git commit -m"test"

 

1. 修改

# git rebase -i HEAD~3
pick 60b3e9b my first commit
pick be6ce3c my second commit
pick 5d9a124 test

修改顺序, 将最新commit放到待修改commit的后面,并将该commit操作改成"squash", 表示与前一次合并:
pick 60b3e9b my first commit
squash 5d9a124 test
pick be6ce3c my second commit


保存退出
Ctl + O (Write Out) -> 回车
Ctl + X (Exit)

 

3. 提交修改

3 # git rebase --continue
Successfully rebased and updated refs/heads/add-upload-api.

4. # git push origin xxx (maybe need -f)

 

 场景3:为commit增加body信息

完整的commit message包含title(必选), body(可选), footbar(可选)

  • 常用方式:只提交title字段,如下
#git commit -m"这是一个title"
  • 官方方式:编写message文档,然后提交

    参考:https://cbea.ms/git-commit/#separate

                         https://cbea.ms/git-commit/#separate

 

  • 补救方式:命令行只提交title后增加body
# git commit --amend
  <这是一个title>

  <在这里可以增加你的body>

 

场景4:为中间某一次(比如,倒数第二次)的commit修改 commit body信息

# git rebase -i HEAD~3

pick ea95d985 Add exclude tag: requires-local
reword 0e2aca51 Add timer for dpp onboarding when mutiple-engine enable
pick 6155e967 Abstract some common code about database operation

 

 场景5:将当前修改commit --amend到前一次的错误commit上

描述:在目的分支上,多提交了一次本该不属于本分支的commit,

           之后又在此分支上修改,commit的时候不小心使用了commit --amend,于是目前的状况是这样的

---第三次,也应该是本分支的修改,但是使用了 git commit --amend, 于是此次修改被与第二次commit合并了
303430e (HEAD -> mybranch) The second wrong commit    ---第二次错误的提交
2ec5532 The First right commit   ---第一次正常的提交
e4dd64b base...

 

目标:将第三次修改和第二次的commit合并分立,然后删除掉第二次合并,将第三次的修改归入第一次commit

 

操作步骤

# git reset --soft HEAD~1


# git status
…
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   app/app.go
    …
        modified:   app/xxx.go    //第二次错误的提交也在缓存区中


# 剔除第二次的修改,然后重新commit --amend

 

 

 

二. Push


 

命令标准:

git push <remote 名字> <本地分支的名字> : <远程库的名字>

用法1:# git push origin HEAD:refs/for/master

【解析】

origin : 是远程的库的名字
HEAD: 是一个特别的指针,它是一个指向你正在工作的本地分支的指针,可以把它当做本地分支的别名,git这样就可以知道你工作在哪个分支
refs/for :意义在于我们提交代码到服务器之后是需要经过code review 之后才能进行merge的
refs/heads 不需要
【实操场景】

 

 三. 综合


 

场景1. Git rebase与修改author的综合修改

【描述】:
Master -> branch 1 -> branch 1'
首先,我在branch 1' 上提交了一个具有3个commit 的merge request到branch 1上
然后,branch 1的autho: author1取出这个3个commit,以及其他的commit,一起提交另一个mr到master分支上
最后,切换到branch 1上可以看到如下的commit信息

# git log --oneline
b7d6f49 (HEAD -> branch 1, origin/branch 1) Refactor the reconciler of cr2…
2e34ef2 Refactor the reconciler of cr1 …
47e0eac Modify the crd definition …
02bfcc0 Tftp-init-container support for new CRD and 'types.go'
526616d Agent support for new CRD and 'types.go'
c326835 Common 'types.go' and CRD changes
f4b5b5d (origin/master, origin/HEAD, master) Update obsolete term in document ----源自master,为这次mr的base

【问题】: 在上面的mr中,由于错误操作,将前三个commit的author错误的设置成author1, 但实际的original author为author 2
【需求】: author2需要将这三个commit的author改成自己

 

操作步骤:
# git checkout branch1
# git pull origin --rebase
# git rebase --skip    ##如果有冲突,但是我只想直接完全branch1,就可以使用该命令跳过这些可能的冲突
# git rebase -i HEAD~3
edit 1e15ba1 Modify the crd …
edit 99f0e8e Refactor the reconciler of cr1
edit 5d4341a Refactor the reconciler ofcr2

保存+ 退出
Stopped at 47e0eac...  Modify the crd definition and corresponding structure to support multi node/rannic
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue


# git commit --amend --author="Author2 Name <x@xxxx.com>" --no-edit
[detached HEAD 1e15ba1] Modify the crd definition …
 Author: Author1
…
# git rebase --continue
Stopped at 2e34ef2...  Refactor the reconciler of cr1
…
# git rebase --continue
# git commit --amend --author="Author2 <x@xxxx.com>" --no-edit
# git rebase --continue

# git push origin 172-types-go-support-multi-rannic-phase1 -f

 

posted @ 2021-11-17 17:35  水鬼子  阅读(77)  评论(0编辑  收藏  举报