一起踩過的那些git的坑

1. 測試小姐姐 趕緊修了這個bug??,什么鬼?

我沒有提交代碼啊!
我沒有提交代碼啊!
我沒有提交代碼啊!

重要的事情說三遍;查看git log 發(fā)現(xiàn),author 真的是我哦!于是作死又想,小五用了 我的電腦提交代碼了!??

遇到這種情況要怎么處理呢?我們一步步來說

a. 查看電腦的git config 配置

$ git config --list
config.png

查看終端輸出的配置信息中的 user.name 和 user.email 是不是你的名字;如果不是,則可以用下面的命令進行修改:

$ git config --global user.name myname
$ git config --global user.email myemialname

然后再通過 git config --list 命令查看,你會發(fā)現(xiàn)git 配置信息已經(jīng)被變更了;

b. 修改遠程倉庫上提交的記錄

查看提交記錄

$ git log

如果你提交的commit剛好是當(dāng)前HEAD,那么恭喜你,會方便很多,命令如下

$ git commit --amend --author "myname<myemialname>" -m "你之前commit時提交的描述信息"
$ git fetch
$ git push --force-with-lease origin branchname

??注意:這里一定要先fetch 遠程倉庫的代碼下來,如果有沖突的話,就解決下沖突,然后在push.

如果是第二條記錄,如下所示,要修改第二條記錄的Author,


log.png

則需要執(zhí)行下面的命令

$ git rebase -i HEAD~2
pick db11001 test 111111
pick 82a8194 test 2222222

# Rebase bda891e..82a8194 onto bda891e (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
~                                                                                                                             
~      

將第二條的 pick 修改為 edit,保存退出,此時終端如下所示:

Stopped at 82a8194...  test 2222222
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue

此時從新提交 commit 信息

$ git commit --amend --author "mynamechange<myemailchange>" -m "test 222222"
[detached HEAD 196e5cd] test 222222
 Author: mynamechange <myemailchange>
 Date: Tue Mar 26 20:50:32 2019 +0800
 1 file changed, 3 insertions(+), 1 deletion(-)
$ git rebase --continue
Successfully rebased and updated refs/heads/master.
$ git push -f

此時再查看 log ,發(fā)現(xiàn)已經(jīng)被修改了,但是變成了最頂?shù)囊粭lcommit記錄了,commit id 也發(fā)生了變化,commit的內(nèi)容沒有發(fā)生變化;

commit 49f5a2bcc90b53493a7efdba2b323028c3355519 (HEAD -> master, origin/master, origin/HEAD)
Author: mynamechange <myemailchange>
Date:   Tue Mar 26 20:50:32 2019 +0800

    test 222222

commit db1100183a5eb6f17b5ddfb6254533461d7fee5c
Author: myname <myemialname>
Date:   Tue Mar 26 20:49:44 2019 +0800

    test 111111

commit bda891e62d92d331fb7ab28c1afba0aa4572b5a2

2. 腦子短路一下子手賤 提交了測試代碼到遠程??

??這種情況,最好是第一時間能夠發(fā)現(xiàn)問題,要不然等別人提交了代碼,再做抹除就會把其他人的提交也一并抹除掉了!

查看提交記錄對應(yīng)的commit id

$ git log

然后通過reset 命令,將本地倉庫的代碼回到 指定的 commit id;這個commit id 之后提交的代碼則會全部被reset掉;
所以

這個命令慎用!
這個命令慎用!
這個命令慎用!

命令如下所示:

$ git reset --hard dd98a8ea02dd31b8eb97bd686c3ebf2ed59a66d7
$ git add .
$ git commit -m "test reset"
$ git push origin HEAD --force

這個命令會將之前提交的記錄直接抹掉,并且不會產(chǎn)生新的commit 記錄;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容