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

我试过:

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

其他回答

上面的所有提示都是正确的,但很多时候,为了真正重新设置您的项目,您还需要删除即使是您的.gitignore 中的文件。

要从远程中删除您的项目目录和重新克隆的道德等级是:

git fetch
git reset --hard
git clean -x -d -f

警告: git clean -x -d -f 是不可逆转的,您可能会失去文件和数据(例如,您使用.gitignore 忽略的东西)。

使用下面的命令. 这些命令将从本地 git 删除所有未追踪的文件

git fetch origin
git reset --hard origin/master
git clean -d -f

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

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

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

这是我经常面对的事情,我已经普遍化了上面的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 事,所以在 GitHub 中检查)然后在下面的步骤4中取代主或主,以此保存当前事物 git stash -u 更新从远程 git fetch 起源重新设置到远程默认分支(但参见上面的步骤1) git 重新设置 --hard 起源/main