簡體   English   中英

git stash -> 將隱藏的更改與當前更改合並

[英]git stash -> merge stashed change with current changes

我對我的分支進行了一些更改,並意識到我忘記了我已經對所述分支進行了一些其他必要的更改。 我想要的是一種將我隱藏的更改與當前更改合並的方法。

有沒有辦法做到這一點?

更多的是為了方便,我最終放棄並首先提交了我當前的更改,然后是我隱藏的更改,但我更願意一舉將它們納入其中。

tl;博士

首先運行git add


我剛剛發現,如果您未提交的更改被添加到索引中(即“暫存”,使用git add ... ),那么git stash apply (並且,大概是git stash pop )實際上會進行適當的合並。 如果沒有沖突,你就是金子。 如果沒有,請像往常一樣使用git mergetool或使用編輯器手動解決它們。

需要明確的是,這是我正在談論的過程:

mkdir test-repo && cd test-repo && git init
echo test > test.txt
git add test.txt && git commit -m "Initial version"

# here's the interesting part:

# make a local change and stash it:
echo test2 > test.txt
git stash

# make a different local change:
echo test3 > test.txt

# try to apply the previous changes:
git stash apply
# git complains "Cannot apply to a dirty working tree, please stage your changes"

# add "test3" changes to the index, then re-try the stash:
git add test.txt
git stash apply
# git says: "Auto-merging test.txt"
# git says: "CONFLICT (content): Merge conflict in test.txt"

......這可能是你正在尋找的。

運行git stash popgit stash apply本質上是一個合並。 您應該不需要提交當前的更改,除非存儲中更改的文件也在工作副本中更改,在這種情況下,您會看到以下錯誤消息:

error: Your local changes to the following files would be overwritten by merge:
       file.txt
Please, commit your changes or stash them before you can merge.
Aborting

在這種情況下,您無法一步將 stash 應用到您當前的更改。 如果您真的不想要兩次提交,您可以提交更改,應用存儲,再次提交,並使用git rebase這兩個提交,但這可能會帶來更多的麻煩,但值得。

我想要的是一種將我隱藏的更改與當前更改合並的方法

這是執行此操作的另一種選擇:

git stash show -p|git apply
git stash drop

git stash show -p將顯示上次保存的存儲的補丁。 git apply將應用它。 合並完成后,可以使用git stash drop刪除合並的存儲。

我這樣做的方法是先git add這個然后git stash apply <stash code> 這是最簡單的方法。

正如@Brandan 所建議的,這是我需要做的才能繞過

error: Your local changes to the following files would be overwritten by merge:
       file.txt
Please, commit your changes or stash them before you can merge.
Aborting

按照這個過程:

git status  # local changes to `file`
git stash list  # further changes to `file` we want to merge
git commit -m "WIP" file
git stash pop
git commit -m "WIP2" file
git rebase -i HEAD^^  # I always use interactive rebase -- I'm sure you could do this in a single command with the simplicity of this process -- basically squash HEAD into HEAD^
# mark the second commit to squash into the first using your EDITOR
git reset HEAD^

並且您將得到完全合並的本地更改file ,准備做進一步的工作/清理或進行一次良好的提交。 或者,如果您知道file的合並內容是正確的,您可以寫一條合適的消息並跳過git reset HEAD^

可能是,從......合並(通過difftool)並不是最糟糕的想法......是的......一個分支!

> current_branch=$(git status | head -n1 | cut -d' ' -f3)
> stash_branch="$current_branch-stash-$(date +%yy%mm%dd-%Hh%M)"
> git stash branch $stash_branch
> git checkout $current_branch
> git difftool $stash_branch

你可以很容易地

  1. 提交您當前的更改
  2. 解開你的藏匿處並解決沖突
  3. 從 stash 提交更改
  4. 軟重置以提交您來自(最后一次正確提交)

我找到了另一個解決方案。 您可以提交當前打開的更改,然后彈出您的存儲,然后軟重置到上次提交之前。

git commit -am 'Open changes'
git stash pop
git reset --soft HEAD~1

另一種選擇是對本地未提交的更改進行另一個“git stash”,然后組合兩個 git stash。 不幸的是,git 似乎沒有辦法輕松地組合兩個存儲。 因此,一種選擇是創建兩個 .diff 文件並同時應用它們 - 至少它不是額外的提交並且不涉及十步過程:|

如何: https : //stackoverflow.com/a/9658688/32453

暫無
暫無

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

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