git 将本地存储库分支重置为就像远程存储库 HEAD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1628088/
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
Reset local repository branch to be just like remote repository HEAD
提问by hap497
How do I reset my local branch to be just like the branch on the remote repository?
如何将我的本地分支重置为就像远程存储库上的分支一样?
I did:
我做了:
git reset --hard HEAD
But when I run a git status
,
但是当我运行一个git status
,
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: java/com/mycompany/TestContacts.java
modified: java/com/mycompany/TestParser.java
Can you please tell me why I have these 'modified'? I haven't touched these files? If I did, I want to remove those.
你能告诉我为什么我有这些“修改”吗?我没有动过这些文件?如果我这样做了,我想删除它们。
回答by Dan Moulding
Setting your branch to exactly match the remote branch can be done in two steps:
将分支设置为与远程分支完全匹配可以分两步完成:
git fetch origin
git reset --hard origin/master
If you want to save your current branch's state before doing this (just in case), you can do:
如果您想在执行此操作之前保存当前分支的状态(以防万一),您可以执行以下操作:
git commit -a -m "Saving my work, just in case"
git branch my-saved-work
Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch).
现在,您的工作已保存在“my-saved-work”分支上,以防您决定将其恢复(或想稍后查看它或将其与更新的分支进行比较)。
Note that the first example assumes that the remote repo's name is "origin" and that the branch named "master" in the remote repo matches the currently checked-out branch in your local repo.
请注意,第一个示例假定远程仓库的名称是“origin”,并且远程仓库中名为“master”的分支与本地仓库中当前签出的分支相匹配。
BTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Did you recently push into your local repo? If not, then no worries -- something else must have caused these files to unexpectedly end up modified. Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).
顺便说一句,您所处的这种情况看起来很像一种常见情况,其中已将推送推送到非裸存储库的当前已检出分支。你最近有没有推送到你的本地仓库?如果没有,那么不用担心——一定是其他原因导致这些文件意外地被修改了。否则,您应该意识到不建议推送到非裸存储库(尤其是不要推送到当前签出的分支)。
回答by Akavall
I needed to do (the solution in the accepted answer):
我需要做(已接受答案中的解决方案):
git fetch origin
git reset --hard origin/master
Followed by:
其次是:
git clean -f
To see what files will be removed (without actually removing them):
要查看将删除哪些文件(而不实际删除它们):
git clean -n -f
回答by Acumenus
First, reset to the previously fetched HEAD
of the corresponding upstream branch:
首先,重置为之前获取HEAD
的相应上游分支:
git reset --hard @{u}
The advantage of specifying @{u}
or its verbose form @{upstream}
is that the name of the remote repo and branch don't have to be explicitly specified.
指定@{u}
或其详细形式的优点@{upstream}
是不必明确指定远程仓库和分支的名称。
Next, as needed, remove untracked files, optionally also with -x
:
接下来,根据需要,删除未跟踪的文件,也可选择使用-x
:
git clean -df
Finally, as needed, get the latest changes:
最后,根据需要,获取最新的更改:
git pull
回答by Mikael Ohlson
git reset --hard HEAD
actually only resets to the last committed state. In this case HEAD refers to the HEAD of your branch.
git reset --hard HEAD
实际上只重置到最后提交的状态。在这种情况下, HEAD 是指您的分支的 HEAD。
If you have several commits, this won't work..
如果你有几次提交,这将不起作用..
What you probably want to do, is reset to the head of origin or whatever you remote repository is called. I'd probably just do something like
您可能想要做的是重置为源头或远程存储库调用的任何内容。我可能会做类似的事情
git reset --hard origin/HEAD
Be careful though. Hard resets cannot easily be undone. It is better to do as Dan suggests, and branch off a copy of your changes before resetting.
不过要小心。硬重置无法轻易撤消。最好按照 Dan 的建议去做,并在重置之前分出一份更改的副本。
回答by Christopher Smith
All of the above suggests are right, but often to reallyreset your project, you also need to remove even files that are in your .gitignore
.
以上所有建议都是正确的,但通常要真正重置您的项目,您还需要删除.gitignore
.
To get the moral equivalent of erasing your project directory and re-cloningfrom the remote is:
要获得擦除项目目录并从远程重新克隆的道德等价物是:
git fetch
git reset --hard
git clean -x -d -f
Warning: git clean -x -d -f
is irreversibleand you may lose files and data (e.g. things you have ignored using .gitignore
).
警告:git clean -x -d -f
是不可逆的,您可能会丢失文件和数据(例如您使用 忽略的内容.gitignore
)。
回答by Jamsheer
Use the commands below. These commands will remove all untracked files from local git too
使用以下命令。这些命令也将从本地 git 中删除所有未跟踪的文件
git fetch origin
git reset --hard origin/master
git clean -d -f
回答by Robert Siemer
The question mixes two issues here:
这个问题在这里混合了两个问题:
- how to reset a local branch to the point where the remote is
- how to clear your staging area (and possibly the working directory), so that
git status
saysnothing to commit, working directory clean.
- 如何将本地分支重置到远程所在的位置
- 如何清除您的暂存区(可能还有工作目录),这样
git status
说nothing to commit, working directory clean.
The one-stop-answer is:
一站式答案是:
git fetch --prune
(optional) Updates the local snapshot of the remote repo. Further commands are local only.git reset --hard @{upstream}
Puts the local branch pointer to where the snapshot of the remote is, as well as set the index and the working directory to the files of that commit.git clean -d --force
Removes untracked files and directories which hinder git to say “working directory clean”.
git fetch --prune
(可选)更新远程仓库的本地快照。进一步的命令只是本地的。git reset --hard @{upstream}
将本地分支指针指向远程快照所在的位置,并将索引和工作目录设置为该提交的文件。git clean -d --force
删除阻碍 git 说出“工作目录清理”的未跟踪文件和目录。
回答by Andrew Tulloch
This is something I face regularly, & I've generalised the script Wolfgang provided above to work with any branch
这是我经常遇到的事情,并且我已经将上面提供的 Wolfgang 脚本概括为适用于任何分支
I also added an "are you sure" prompt, & some feedback output
我还添加了一个“你确定”提示和一些反馈输出
#!/bin/bash
# reset the current repository
# WF 2012-10-15
# AT 2012-11-09
# see http://stackoverflow.com/questions/1628088/how-to-reset-my-local-repository-to-be-just-like-the-remote-repository-head
timestamp=`date "+%Y-%m-%d-%H_%M_%S"`
branchname=`git rev-parse --symbolic-full-name --abbrev-ref HEAD`
read -p "Reset branch $branchname to origin (y/n)? "
[ "$REPLY" != "y" ] ||
echo "about to auto-commit any changes"
git commit -a -m "auto commit at $timestamp"
if [ $? -eq 0 ]
then
echo "Creating backup auto-save branch: auto-save-$branchname-at-$timestamp"
git branch "auto-save-$branchname-at-$timestamp"
fi
echo "now resetting to origin/$branchname"
git fetch origin
git reset --hard origin/$branchname
回答by eigenharsha
Provided that the remote repository is origin
, and that you're interested in branch_name
:
前提是远程存储库是origin
,并且您感兴趣branch_name
:
git fetch origin
git reset --hard origin/<branch_name>
Also, you go for reset the current branch of origin
to HEAD
.
此外,您将重置origin
to的当前分支HEAD
。
git fetch origin
git reset --hard origin/HEAD
How it works:
这个怎么运作:
git fetch origin
downloads the latest from remote without trying to merge or rebase anything.
git fetch origin
从远程下载最新版本,而无需尝试合并或重新设置任何内容。
Then the git reset
resets the <branch_name>
branch to what you just fetched. The --hard
option changes all the files in your working tree to match the files in origin/branch_name
.
然后将分支git reset
重置为<branch_name>
您刚刚获取的内容。该--hard
选项更改工作树中的所有文件以匹配origin/branch_name
.
回答by Wolfgang Fahl
Here is a script that automates what the most popular answer suggests ... See https://stackoverflow.com/a/13308579/1497139for an improved version that supports branches
这是一个脚本,可以自动执行最流行的答案所建议的内容……有关支持分支的改进版本,请参阅https://stackoverflow.com/a/13308579/1497139
#!/bin/bash
# reset the current repository
# WF 2012-10-15
# see https://stackoverflow.com/questions/1628088/how-to-reset-my-local-repository-to-be-just-like-the-remote-repository-head
timestamp=`date "+%Y-%m-%d-%H_%M_%S"`
git commit -a -m "auto commit at $timestamp"
if [ $? -eq 0 ]
then
git branch "auto-save-at-$timestamp"
fi
git fetch origin
git reset --hard origin/master