我如何将我的本地分支重新设置为远程存储库的分支类似?

我试过:

git reset --hard HEAD

但 git 状态声明我有修改的文件:

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

当前回答

这是我经常面对的事情,我已经普遍化了上面的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

其他回答

难道你忘了创建一个功能分支,并通过错误直接指责主人吗?

您现在可以创建功能分支,并在不影响工作组(本地文件系统)的情况下重新设置主管,以避免启动构建、测试和文件锁问题:

git checkout -b feature-branch
git branch -f master origin/master

扔错误,因为它没有进行更改。

所以,你可以使用 git stash

这将保存未完成的更改,以便以后使用,然后将其从工作副本中逆转。 如果您想要这些更改,您可以使用 git stash 应用程序

您可以使用 git pull。

这需要最近的代码从远程 repo。

这是我经常面对的事情,我已经普遍化了上面的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

首先,请使用 git 状态检查您是否有任何本地变更。

然后执行:

git fetch
git reset --hard @{push}

它将重新设置当前的本地分支到相同的远程分支,将用于 git push. 这特别有用,当 git config push.default 电流设置时. 例如,当您的分支是 abc 和远程是起源时,它将重新设置为起源/abc。

请参见 Git 评论,了解更多关于 @{push} 的详细信息

确保远程存储库的起源,并且您对分支_name感兴趣:

git fetch origin
git reset --hard origin/<branch_name>

此外,您将重新设置现有起源分支到头部。

git fetch origin
git reset --hard origin/HEAD

它是如何工作的:

git fetch 起源从远程下载最新的,而无需尝试合并或拆除任何东西。

然后 git 重新设置将 <branch_name> 分支重新设置为您刚刚抓住的。 --hard 选项将您的工作树中的所有文件更改以匹配原始/branch_name 中的文件。