簡體   English   中英

Git相當於hg更新

[英]Git equivalent to hg update

以下是Mercurial的一個小例子,與Git類似。 我無法理解如何使用Git進行hg update

我有一個小的Mercurial設置,有4個提交 - 我退后一個提交

hg init
echo "1" > a.txt; hg commit -A -m "1. commit" a.txt
echo "2" >> a.txt; hg commit -m "2. commit" a.txt
echo "3" >> a.txt; hg commit -m "3. commit" a.txt
echo "4" >> a.txt; hg commit -m "4. commit" a.txt
hg update -r 3
thg # or hg view`

這給了這張照片

THG

請注意,我看到所有四個提交 - 即前歷史和后續提交

讓我嘗試使用Git做同樣的例子

git init
echo "1" > a.txt; git add a.txt; git commit  -m "1. commit" a.txt
echo "2" >> a.txt; git commit -m "2. commit" a.txt
echo "3" >> a.txt; git commit -m "3. commit" a.txt
echo "4" >> a.txt; git commit -m "4. commit" a.txt # gives for me [master 57bb375]

讓我看看提交:

git log --graph --pretty=format:'%h -%d %s (%cr) <%an>' 

 * 57bb375 - (HEAD, master) 4. commit (14 minutes ago) <Peter Toft>
 * 724a493 - 3. commit (14 minutes ago) <Peter Toft>
 * bb38732 - 2. commit (14 minutes ago) <Peter Toft>
 * 879c593 - 1. commit (15 minutes ago) <Peter Toft>

好 - 按預期四次提交。 讓我回去一次提交(類似於hg更新)

git checkout 724a493

現在git日志怎么樣?

git log --graph --pretty=format:'%h -%d %s (%cr) <%an>' 

 * 724a493 - (HEAD) 3. commit (19 minutes ago) <Peter Toft>
 * bb38732 - 2. commit (19 minutes ago) <Peter Toft>
 * 879c593 - 1. commit (19 minutes ago) <Peter Toft>

gitk還會顯示前3個提交?

所以,“混帳結賬” 不僅僅是類似“汞柱更新”。 以下提交在哪里?

hg update -r 3為您提供了一個可以繼續提交的自動分支頭。 在git中, checkout會將您帶到正確的提交,但不會為您提供新的分支頭。 如果你想要,你可以說

git checkout -b new_branch_name 724a493

當然使用你喜歡的任何名稱。 如果你不使用-b ,就像你的問題一樣,你會進入一個分離的HEAD的狀態......這正是hg update -r 3git checkout 724a493之間的區別。 注意結帳時Git打印的消息(從我運行你的例子):

Note: checking out '2ffb5e0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 2ffb5e0... 3. commit

簡單地說,當你運行git checkout 724a493時, 57bb375提交沒有去任何地方。 您看到的行為只是git log只顯示作為簽出提交的祖先的提交。

在Mercurial術語中, git log

$ hg log -r ::.

這意味着“向我展示作為.祖先的提交,即工作副本父版本”。 要在Git中獲得等效的hg log ,只需運行即可

$ git log --all

這個小差異是Git的一個關鍵特性 ,因為它允許您從許多其他存儲庫中提取提交,而不會默認情況下看到它們。 只有當您簽出分支(或您的案例中的提交)時,您才會看到已下載到存儲庫中的提交。

另一種解決方案:添加

[alias]
    restore = "!f() { git checkout $(git rev-list -n 1 HEAD -- $1)~1 -- $(git diff --name-status $(git rev-list -n 1 HEAD -- $1)~1 | grep ^D | cut -f 2); }; f"

那么“git restore”會讓我向后移動一個提交,而不需要我做一個人工分支。

如果我想返回主分支

git checkout master

暫無
暫無

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

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