反转 git fetch

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15254480/
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 08:16:30  来源:igfitidea点击:

Reverse a git fetch

gitgit-fetch

提问by user2141094

I have a thorough understanding of git, and the difference between pull, fetch, and merge. I have a remote that I track, fetch, and merge with occasionally, let say it's origin/master. What I'm looking to do is reverse the behavior of a 'git fetch'. It sounds goofy, but I want to un-update where my remote tracking branch points, to an older state, the state right before the last fetch. Is this possible?

我对 git 以及 pull、fetch 和 merge 之间的区别有一个透彻的了解。我有一个遥控器,我偶尔会跟踪、获取和合并,假设它是原点/主控。我要做的是逆转“git fetch”的行为。这听起来很愚蠢,但我想取消更新我的远程跟踪分支指向的旧状态,即上次获取之前的状态。这可能吗?

For example, lets say this is my workflow...

例如,假设这是我的工作流程......

git show origin/master # shows commit abc123

git fetch              # yay i got something!

git show origin/master # shows commit def456

mystery command goes here so that...

神秘命令放在这里,以便...

git show origin/master # shows commit abc123

It's kind of a weird thing to want, but I have a crontab that watches a git repo to detect when there's something to fetch, and I'm having issues debugging the script that performs some actions based on this behavior. Instead of waiting for origin/master to change, I'd like to change it myself so I can debug my script!

这有点奇怪,但我有一个 crontab,它监视 git repo 以检测何时有要获取的东西,并且我在调试基于此行为执行某些操作的脚本时遇到问题。与其等待 origin/master 改变,我想自己改变它,这样我就可以调试我的脚本了!

采纳答案by jthill

You want

你要

git update-ref refs/remotes/origin/master refs/remotes/origin/master@{1}

git update-ref refs/remotes/origin/master refs/remotes/origin/master@{1}

update-refwants the full spell on the ref it's updating because it's (much) lower level than the commands that respect ref-naming conventions.

update-ref想要在它正在更新的 ref 上完整拼写,因为它比尊重 ref 命名约定的命令(远)低级别。

回答by Tasos Papastylianou

This isn't a "reverse a git fetch" answer, but it seems your actualproblem is how to programmatically "watch a repo for changes" without necessarily altering your repository in any way.

这不是“反向 git fetch”答案,但您的实际问题似乎是如何以编程方式“观察存储库的更改”而不必以任何方式更改存储库。

For this, you can use git fetch --dry-run. A dry run will not cause any changes to your repository, but if there are changes in the remote, then it will have some basic terminal output; if there are no changes there will not be any output. If you want to have this running as an automated script instead of a manual check, it should be relatively straightforward to create a simple bash script that tests git fetch --dry-runfor output.

为此,您可以使用git fetch --dry-run. 试运行不会对您的存储库造成任何更改,但是如果远程有更改,那么它将具有一些基本的终端输出;如果没有更改,则不会有任何输出。如果您想让它作为自动脚本运行而不是手动检查,那么创建一个简单的 bash 脚本来测试git fetch --dry-run输出应该相对简单。

回答by Dewey Sasser

I'm not sure if there's an actual git command to do it, and this is a big hackish, but...

我不确定是否有实际的 git 命令来执行此操作,这是一个很大的 hackish,但是...

echo $OLD_COMMIT > .git/refs/remotes/origin/master

should work

应该管用

回答by Stephan

I hope this time I got what you want and I think it's this:

我希望这次我得到了你想要的,我想是这样的:

git rebase origin/master --onto origin/master^