git 如何存储本地存储库和远程存储库之间的更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33069752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:26:49  来源:igfitidea点击:

How to stash the changes between local repository and remote repository

gitgithub

提问by Nu2Overflow

I want to stash all the changes between local repository and remote origin/master. There are some commits made in the local repository at the same time some other changes made in remote repository.

我想隐藏本地存储库和远程源/主服务器之间的所有更改。在本地存储库中进行了一些提交,同时在远程存储库中进行了一些其他更改。

  • I want to stash the differences between local and remote.
  • Discard all the local commits
  • Bring the local repository to in sync with remote.
  • Apply my stash changes.
  • 我想隐藏本地和远程之间的差异。
  • 丢弃所有本地提交
  • 使本地存储库与远程同步。
  • 应用我的存储更改。

How can I achieve this using git commands?

如何使用 git 命令实现这一目标?

回答by Schwern

I want to stash all the changes between local repository and remote origin/master.

我想隐藏本地存储库和远程源/主服务器之间的所有更改。

"stash" has a special meaning in Git, git stashputs uncommitted changesin a special commit for retrieval later. It's used when you have some work that's not ready to be committed, but you need to do something to the repository like checkout another branch or push or pull. For the rest of this answer I will use "stash" to mean git stash.

“stash”在 Git 中具有特殊含义,git stash未提交的更改放在一个特殊的提交中以供稍后检索。当您有一些工作尚未准备好提交时使用它,但您需要对存储库执行某些操作,例如签出另一个分支或推送或拉取。对于这个答案的其余部分,我将使用“stash”来表示git stash.

There are some commits made in the local repository at the same time some other changes made in remote repository.

在本地存储库中进行了一些提交,同时在远程存储库中进行了一些其他更改。

This is situation normal.

这是正常情况。

I want to stash the differences between local and remote. Discard all the local commits

我想隐藏本地和远程之间的差异。丢弃所有本地提交

These steps are not necessary. Git can take care of this. See below.

这些步骤不是必需的。Git 可以解决这个问题。见下文。

Bring the local repository to in sync with remote.

使本地存储库与远程同步。

You get an up to date version of the remote repository with git fetch origin(assuming the remote is named origin). This is safe and will not modify any local branches.

您将获得远程存储库的最新版本git fetch origin(假设远程名为origin)。这是安全的,不会修改任何本地分支。

Apply my stash changes.

应用我的存储更改。

There's no need to move your local commitsout of the way. Git can merge your local work with the remotes. However if you have uncommitted workyou're going to want to stash it. git stash savewill squirrel your uncommitted work away for later.

没有必要将您的本地提交移开。Git 可以将您的本地工作与遥控器合并。但是,如果您有未提交的工作,您会想要隐藏它。git stash save将您未完成的工作松散以备后用。

After fetching you can git merge origin/masterto merge your work with the remote or git rebase origin/masterto put all your changes on top of the remote.

获取后,您可以git merge origin/master将您的工作与遥控器合并或git rebase origin/master将所有更改放在遥控器之上。

You can do the fetch and merge/rebase in a single command. To fetch and merge use git pull origin master. To fetch and rebase use git pull --rebase origin master.

您可以在单个命令中执行获取和合并/变基。要获取和合并使用git pull origin master. 要获取和变基使用git pull --rebase origin master.

Now that you're up to date you can git stash popto restore your stashed changes.

现在您是最新的,您可以git stash pop恢复您隐藏的更改。

git pull --rebaseis the normal way you should be updating your local branches with work from the remote. A lot of merging that's nothing but updating the remote will result in a confused history, rebasing keeps everything nice and linear. You don't need to tell it the remote and branch, Git can usually figure this out for you. The normal workflow looks like this.

git pull --rebase是您应该使用远程工作更新本地分支的正常方式。很多合并只是更新遥控器会导致历史混乱,重新定位使一切保持良好和线性。你不需要告诉它远程和分支,Git 通常可以为你解决这个问题。正常的工作流程是这样的。

  • git stash saveif you have uncommitted work.
  • git pull --rebaseto get updates from the remote and apply them
  • git stash popto restore your uncommitted work.
  • git stash save如果你有未完成的工作。
  • git pull --rebase从远程获取更新并应用它们
  • git stash pop恢复您未完成的工作。

回答by Chad Bingham

git stashwill stash your changes in a dirty working directory

git stash将您的更改存储在一个脏的工作目录中

git stash popRemove a single stashed state from the stash list and apply it on top of the current working tree state.

git stash pop从存储列表中删除单个隐藏状态并将其应用于当前工作树状态的顶部。

git diff masterwill show the differences.

git diff master将显示差异。

To see more stash options: git stash --help

要查看更多存储选项: git stash --help

EDIT

编辑

Edit made for your revised question:

对您修改后的问题进行了编辑:

  1. I want to stash the differences between local and remote.

  2. Discard all the local commits

  3. Bring the local repository to in sync with remote.

  4. Apply my stash changes.

  1. 我想隐藏本地和远程之间的差异。

  2. 丢弃所有本地提交

  3. 使本地存储库与远程同步。

  4. 应用我的存储更改。

First of all, number 2 will conflict with number 4. So I will explain how to update with remote by stashing:

首先,2 号会和 4 号冲突。所以我将解释如何通过 stashing 来更新远程:

Stash your changes. This will remove them from your current working directory. git stash

隐藏您的更改。这将从您当前的工作目录中删除它们。 git stash

Pull your changes from remote git pull origin <branch>

从远程拉取您的更改 git pull origin <branch>

Pop your changes back. git stash pop

弹出您的更改。 git stash pop

NOTE: I recommend having a merge tool installed for this incase you run in to merge conflicts. I prefer P4Merge. If you get merge conflicts: git mergetool

注意:我建议为此安装一个合并工具,以防您遇到合并冲突。我更喜欢 P4Merge。如果遇到合并冲突: git mergetool

If you are wanting to know how to do these individually:

如果您想知道如何单独执行这些操作:

  1. I want to stash the differences between local and remote. git stash

  2. Discard all the local commits I just do git stashand forget about my stash. I don't know if this is preferred or not. I think you can "drop" your stash change if you need.

  3. Bring the local repository to in sync with remote. git stashgit pull origin <branch> git stash pop`

  4. Apply my stash changes. If you want to keep your stash on the stash list: git stash applyif you want to remove the stash from the stash list: git stash pop

  1. 我想隐藏本地和远程之间的差异。 git stash

  2. 丢弃我刚刚做的所有本地提交git stash,忘记我的藏匿处。我不知道这是否是首选。如果需要,我认为您可以“放弃”您的藏匿变化。

  3. 使本地存储库与远程同步。 git stashgit pull origin <branch> git stash pop`

  4. 应用我的存储更改。如果您想将您的藏匿处保留在藏匿列表中: git stash apply如果您想从藏匿列表中删除藏匿处: git stash pop

And your all set. Hope this helps

和你的一切。希望这可以帮助