簡體   English   中英

go 如何回到 git 中?

[英]how to go back in git?

這是我的git log的樣子——

Nikhil@admin-PC MINGW32 ~/Desktop/facebook by nick/Addmie/nodejs (messageadded)
$ git log
commit fcf7a0bf45dffdf2da197f84b1b3e4c21715b5f1 (HEAD -> messageadded, origin/messageadded)
Author: codewithnick <nikhilsingh526452@gmail.com>
Date:   Fri Jun 19 00:02:46 2020 +0530

    i have added the message feature it is not live chat yet]

commit 1e5fb40e7deb0787cd8be83b28f39f5acc06b8bf (origin/profileadded, profileadded, ajaxadded)
Author: codewithnick <nikhilsingh526452@gmail.com>
Date:   Sat Jun 13 23:57:23 2020 +0530

    i have seprated profiles

commit 1f8440134b0147efd454b966ffc464ff5843f51e (origin/ajaxadded, exploreadded)
Author: codewithnick <nikhilsingh526452@gmail.com>
Date:   Sat Jun 6 00:37:30 2020 +0530

    6/6/2020 12:37

message added分支中最后一次提交后,我在代碼中犯了一些錯誤。 如何將工作目錄中的所有更改和 go 刪除回過去提交的時間?

Git 為您提供了相當多的選項來解決這個問題

暫時切換到不同的提交

如果你想暫時 go 回到特定的提交。 只是嘗試一下。 您所要做的就是檢查所需的提交:

# This will detach your HEAD, that is, leave you with no branch checked out:
git checkout <hash>

或者,如果您想在那里進行提交,請提前 go 並在您使用時創建一個新分支:

git checkout -b old-state <hash>

到 go 回到你所在的位置,再次檢查你所在的分支。 (如果您進行了更改,就像在切換分支時一樣,您必須酌情處理它們。您可以重置以丟棄它們;您可以存儲、結帳、存儲彈出以將它們隨身攜帶;您可以提交如果你想在那里有一個分支,他們可以去那里的一個分支。)

硬刪除未發布的提交

如果您想嚴格覆蓋您的更改:

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard <hash>

# Alternatively, if there's work to keep:
git stash
git reset --hard <hash>
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.

注意:這將重置您的更改。

使用新提交撤消已發布的提交

另一方面,如果您已經發布了作品,您可能不想重置分支,因為這實際上是在重寫歷史。 在這種情況下,您確實可以還原提交。 對於 Git,revert 有一個非常具體的含義:創建一個帶有反向補丁的提交來取消它。 這樣你就不會重寫任何歷史。

# This will create three separate revert commits:
git revert a867b4af 25eee4ca 0766c053

# It also takes ranges. This will revert the last two commits:
git revert HEAD~2..HEAD

#Similarly, you can revert a range of commits using commit hashes:
git revert a867b4af..0766c053 

# Reverting a merge commit
git revert -m 1 <merge_commit_sha>

# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .

# Then commit.
git commit

一個有用的鏈接是討論還原的 git-scm.com 部分

要撤消/恢復上次提交,您可以使用從git log命令獲得的提交 hash 執行以下操作:

git revert <commit hash>

有幾種方法可以做到這一點。 我將列出其中兩個。

Git 復位

git reset --hard <commit hash>

這會將repo重置為提交hash版本。但是現在遠程源仍然有HEAD指向提交D,如果我們直接使用git push來推送更改,它不會更新遠程repo,我們需要添加一個-f選項強制推動改變。

git push -f

重置是有缺陷的,使用起來不是一個很好的主意。第二種方法是使用

恢復

git revert <commit hash>

這會創建一個新的提交,該提交會恢復先前的提交。 HEAD 將指向新的還原提交。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM