如何将本地分支重置为远程存储库中的分支?
我做了:
git reset --hard HEAD
但是当我跑一个 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
你能告诉我为什么我有这些“修改过的”吗?我没有碰过这些文件?如果我这样做,我想删除它们。
将分支设置为与远程分支完全匹配可以分两步完成:
git fetch origin
git reset --hard origin/master
如果您想在执行此操作之前保存当前分支的状态(以防万一),您可以执行以下操作:
git commit -a -m "Saving my work, just in case"
git branch my-saved-work
现在你的工作被保存在“my-saved-work”分支上,以防你决定要它回来(或者想要稍后查看它或者将它与你更新的分支区分开来)。
请注意,第一个示例假定远程仓库的名称为“origin”,远程仓库中名为“master”的分支与本地仓库中当前已检出的分支匹配。
顺便说一下,你所处的这种情况看起来非常像一种常见的情况,即在非裸存储库的当前检出的分支中已经完成了推送。你最近是否进入了当地的仓库?如果没有,那么不用担心 - 其他必须导致这些文件意外地最终被修改。否则,您应该意识到不建议将其推入非裸存储库(而不是特别是当前已检出的分支)。
我需要做(在接受的答案中的解决方案):
git fetch origin
git reset --hard origin/master
其次是:
git clean -f
删除本地文件
要查看将删除哪些文件(而不实际删除它们):
git clean -n -f
首先,重置为先前获取的 HEAD
对应的上游分支:
git reset --hard @{u}
指定的优点 @{u}
或其冗长的形式 @{upstream}
是不必显式指定远程仓库和分支的名称。
接下来,根据需要删除未跟踪的文件,也可以选择删除 -x
:
git clean -df
最后,根据需要,获取最新的更改:
git pull
git reset --hard HEAD
实际上只重置到最后一个提交状态。在这种情况下,HEAD指的是您的分支的HEAD。
如果你有几个提交,这将无法正常工作..
你可能想要做什么,重置为原始头或任何远程存储库被调用。我可能只是做了类似的事情
git reset --hard origin/HEAD
但要小心。硬重置不容易被撤消。最好像Dan建议的那样做,并在重置之前分出你的更改副本。
所有上述建议都是正确的,但往往是 真 重置你的项目,你甚至需要删除你的文件 .gitignore
。
获得道德等同于 擦除项目目录并重新克隆 从遥控器是:
git fetch
git reset --hard
git clean -x -d -f
警告: git clean -x -d -f
是 不可逆转 并且您可能会丢失文件和数据(例如您忽略使用的内容 .gitignore
)。
这是我经常面对的事情,我已经将上面提供的Wolfgang脚本用于任何分支
我还添加了“你确定”的提示,以及一些反馈输出
#!/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
这是一个自动化最流行的答案建议的脚本...
看到 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
只要远程存储库是 origin
,你感兴趣的 branch_name
:
git fetch origin
git reset --hard origin/<branch_name>
此外,你去重置当前的分支 origin
至 HEAD
。
git fetch origin
git reset --hard origin/HEAD
怎么运行的:
git fetch origin
从远程下载最新版本而不尝试合并或重组任何内容。
然后 git reset
重置 <branch_name>
分支到你刚刚获得的东西。该 --hard
选项更改工作树中的所有文件以匹配其中的文件 origin/branch_name
。