将本地存储库分支重置为就像远程存储库 HEAD 一样

发布于 2024-08-09 20:50:25 字数 361 浏览 10 评论 0原文

如何将本地分支重置为与远程存储库上的分支一样?

我尝试过:

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

How do I reset my local branch to be just like the branch on the remote repository?

I tried:

git reset --hard HEAD

But git status claims I have modified files:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(27

盗梦空间 2024-08-16 20:50:26

如果您不介意保存本地更改,但仍想更新存储库以匹配 origin/HEAD,则可以简单地存储本地更改,然后拉取:

git stash
git pull

If you don't mind saving your local changes, yet still want to update your repository to match origin/HEAD, you can simply stash your local changes and then pull:

git stash
git pull
梦醒时光 2024-08-16 20:50:25

将分支设置为与远程分支完全匹配可以通过两个步骤完成:

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”的分支与本地存储库中当前签出的分支匹配。

顺便说一句,您所处的这种情况看起来非常像常见的情况,即推送已完成到非裸存储库当前签出的分支。您最近是否推送到了本地存储库?如果不是,那么不用担心——一定是其他原因导致这些文件意外地最终被修改。否则,您应该注意,不建议推送到非裸存储库(特别是不要推送到当前签出的分支)。

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).

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.

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).

没有心的人 2024-08-16 20:50:25

首先,使用 git reset 重置为之前获取的 HEAD相应上游分支的名称:

git reset --hard @{u}

指定 @{u} 或其详细形式 @{upstream} 的优点是远程存储库和分支的名称没有予以明确指定。在 Windows 或 PowerShell 中,指定 "@{u}"(带双引号)。

接下来,根据需要,使用 git clean 删除未跟踪的文件,也可以选择使用 -x

git clean -df

最后,根据需要,获取最新的更改:

git pull

First, use git reset to reset to the previously fetched HEAD of the corresponding upstream branch:

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. On Windows or with PowerShell, specify "@{u}" (with double quotes).

Next, as needed, use git clean to remove untracked files, optionally also with -x:

git clean -df

Finally, as needed, get the latest changes:

git pull
落叶缤纷 2024-08-16 20:50:25

我需要做(接受的答案中的解决方案):

git fetch origin
git reset --hard origin/master

其次是:

git clean -f

删除本地文件

要查看将删除哪些文件(而不实际删除它们):

git clean -n -f

I needed to do (the solution in the accepted answer):

git fetch origin
git reset --hard origin/master

Followed by:

git clean -f

to remove local files

To see what files will be removed (without actually removing them):

git clean -n -f
东北女汉子 2024-08-16 20:50:25

git reset --hard HEAD 实际上只重置到最后提交的状态。在这种情况下,HEAD 指的是您的分支的 HEAD。

如果您有多个提交,这将不起作用。

您可能想要做的就是重置为原始头或远程存储库的名称。我可能会做类似

git reset --hard origin/HEAD

“小心一点”之类的事情。硬重置无法轻易撤消。最好按照 Dan 的建议进行操作,并在重置之前分支出更改的副本。

git reset --hard HEAD actually only resets to the last committed state. In this case HEAD refers to the HEAD of your branch.

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.

橘味果▽酱 2024-08-16 20:50:25

上述所有建议都是正确的,但通常要真正重置您的项目,您甚至还需要删除 .gitignore 中的文件。

要获得与从远程删除项目目录并重新克隆相同的道德效果,请执行以下操作:

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

警告git clean -x -d -f 不可逆,您可能会丢失文件和数据(例如,您使用 .gitignore 忽略的内容)。

All of the above suggests are right, but often to really reset your project, you also need to remove even files that are in your .gitignore.

To get the moral equivalent of erasing your project directory and re-cloning from the remote is:

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

Warning: git clean -x -d -f is irreversible and you may lose files and data (e.g. things you have ignored using .gitignore).

〆凄凉。 2024-08-16 20:50:25

使用下面的命令。这些命令也会从本地 git 中删除所有未跟踪的文件

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

Use the commands below. These commands will remove all untracked files from local git too

git fetch origin
git reset --hard origin/master
git clean -d -f
递刀给你 2024-08-16 20:50:25

这个问题在这里混合了两个问题:

  1. 如何将本地分支重置到远程分支
  2. 如何清除暂存区域(可能还包括工作目录),以便git status 说没有什么可提交的,工作目录干净。

一站式答案是:

  1. git fetch --prune (可选)更新远程存储库的本地快照。更多命令仅限本地。
    git reset --hard @{upstream}将本地分支指针指向远程快照所在的位置,并设置索引和工作目录为远程快照的文件
  2. git clean -d --force 删除阻碍 git 说“工作目录干净”的未跟踪文件和目录。

The question mixes two issues here:

  1. how to reset a local branch to the point where the remote is
  2. how to clear your staging area (and possibly the working directory), so that git status says nothing to commit, working directory clean.

The one-stop-answer is:

  1. 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.
  2. git clean -d --force Removes untracked files and directories which hinder git to say “working directory clean”.
我的鱼塘能养鲲 2024-08-16 20:50:25

假设远程存储库是 origin,并且您对 branch_name 感兴趣:

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

此外,您还可以将 origin 的当前分支重置为

git fetch origin
git reset --hard origin/HEAD

工作原理:

git fetch origin 从远程下载最新版本,无需尝试合并或变基任何内容。

然后 git reset 分支重置为您刚刚获取的内容。 --hard 选项更改工作树中的所有文件,以匹配 origin/branch_name 中的文件。

Provided that the remote repository is origin, and that you're interested in branch_name:

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

Also, you go for reset the current branch of 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.

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.

说好的呢 2024-08-16 20:50:25

您可以获取原点并重置来解决问题

 git fetch origin
 git reset --hard origin/main

您可以在执行重置之前保存更改,如下所示,

git stash

重置后,如果您想要恢复更改,您可以简单地运行,

git stash apply

You can fetch the origin and reset to solve the problem

 git fetch origin
 git reset --hard origin/main

You can save your changes before doing the reset like below,

git stash

And after resetting, if you want that changes back, you can simply run,

git stash apply
萤火眠眠 2024-08-16 20:50:25

这是我经常面对的事情,&我已经概括了 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

This is something I face regularly, & I've generalised the script Wolfgang provided above to work with any branch

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
绅刃 2024-08-16 20:50:25

2023 简单解决方案:

git fetch
git reset --hard @{u}

重置到 origin/HEAD 并不总是有效,因为它不一定是当前分支的最新提交。

2023 simple solution :

git fetch
git reset --hard @{u}

Reseting to origin/HEAD won't always work as it's not necessarily the latest commit of your current branch.

平安喜乐 2024-08-16 20:50:25

我做了:

git branch -D master
git checkout master

要完全重置分支


注释,您应该签出到另一个分支才能删除所需的分支

I did:

git branch -D master
git checkout master

to totally reset branch


note, you should checkout to another branch to be able to delete required branch

冬天旳寂寞 2024-08-16 20:50:25

这是一个脚本,可以自动执行最流行的答案的建议......
请参阅 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

Here is a script that automates what the most popular answer suggests ...
See https://stackoverflow.com/a/13308579/1497139 for an improved version that supports branches

#!/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
猫卆 2024-08-16 20:50:25

答案

git clean -d -f

被低估了(-d 删除目录)。
谢谢!

The answer

git clean -d -f

was underrated (-d to remove directories).
Thanks!

水晶透心 2024-08-16 20:50:25

如果你像我一样遇到问题,你已经提交了一些更改,但现在,无论出于何种原因你想摆脱它,最快的方法是使用 git reset ,如下所示

git reset --hard HEAD~2

: 2 个不需要的提交,因此数字为 2。您可以将其更改为您自己的要重置的提交数。

所以回答你的问题 - 如果你比远程存储库 HEAD 提前 5 次提交,你应该运行这个命令:

git reset --hard HEAD~5

请注意,你将丢失所做的更改,所以要小心!

If you had a problem as me, that you have already committed some changes, but now, for any reason you want to get rid of it, the quickest way is to use git reset like this:

git reset --hard HEAD~2

I had 2 not needed commits, hence the number 2. You can change it to your own number of commits to reset.

So answering your question - if you're 5 commits ahead of remote repository HEAD, you should run this command:

git reset --hard HEAD~5

Notice that you will lose the changes you've made, so be careful!

怎会甘心 2024-08-16 20:50:25

前面的答案假设要重置的分支是当前分支(已签出)。在评论中,OP hap497 澄清该分支确实已签出,但这并不是原始问题明确要求的。由于至少存在一个“重复”问题,将分支完全重置为存储库状态,它不假设分支已签出,这里有一个替代方案:

如果分支“mybranch”当前签出,则将其重置为远程分支“myremote/mybranch”的头,您可以使用此 低级 命令:

git update-ref refs/heads/mybranch myremote/mybranch

此方法将签出分支保留为是的,并且工作树未受影响。它只是将 mybranch 的头移动到另一个提交,无论第二个参数给出什么。如果需要将多个分支更新到新的远程头,这尤其有用。

不过,执行此操作时要小心,并使用 gitk 或类似工具来仔细检查源和目标。如果你不小心在当前分支上执行此操作(git 不会阻止你这样做),你可能会感到困惑,因为新分支内容与工作树不匹配,工作树没有改变(要修复,请再次更新分支,到之前的位置)。

Previous answers assume that the branch to be reset is the current branch (checked out). In comments, OP hap497 clarified that the branch is indeed checked out, but this is not explicitly required by the original question. Since there is at least one "duplicate" question, Reset branch completely to repository state, which does not assume that the branch is checked out, here's an alternative:

If branch "mybranch" is not currently checked out, to reset it to remote branch "myremote/mybranch"'s head, you can use this low-level command:

git update-ref refs/heads/mybranch myremote/mybranch

This method leaves the checked out branch as it is, and the working tree untouched. It simply moves mybranch's head to another commit, whatever is given as the second argument. This is especially helpful if multiple branches need to be updated to new remote heads.

Use caution when doing this, though, and use gitk or a similar tool to double check source and destination. If you accidentally do this on the current branch (and git will not keep you from this), you may become confused, because the new branch content does not match the working tree, which did not change (to fix, update the branch again, to where it was before).

把昨日还给我 2024-08-16 20:50:25

这是我经常使用的:

git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force;

请注意,最好不要对本地 master/develop 分支进行更改,而是签出到另一个分支进行任何更改,并在分支名称前面加上更改类型,例如 feat/chore/fix/ 等。因此,您只需要拉取更改,而不需要从 master 推送任何更改。对于其他人贡献的其他分支也是如此。因此,仅当您碰巧将更改提交到其他人已提交的分支并且需要重置时,才应使用上述内容。否则,将来避免推送到其他人推送到的分支,而是通过签出分支签出并推送到所述分支。

如果您想将本地分支重置为上游分支中的最新提交,到目前为止对我有用的是:

检查您的遥控器,确保您的上游和原点符合您的预期,如果不符合预期,则使用 git远程添加上游<插入 URL>,例如您派生的原始 GitHub 存储库的上游,和/或 git 远程添加源<插入派生的 GitHub 存储库的 URL>

git remote --verbose

git checkout develop;
git commit -m "Saving work.";
git branch saved-work;
git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force

在 GitHub 上,您还可以签出与本地分支同名的分支,以便将工作保存在那里,尽管如果原始开发具有与本地保存的工作分支相同的更改,则没有必要这样做。我使用开发分支作为示例,但它可以是任何现有的分支名称。

git add .
git commit -m "Reset to upstream/develop"
git push --force origin develop

然后,如果您需要在存在任何冲突的情况下将这些更改与另一个分支合并,并保留开发中的更改,请使用:

git merge -s recursive -X theirs develop

While use

git merge -s recursive -X ours develop

来保留branch_name的冲突更改。否则,请使用带有 git mergetool 的合并工具。

将所有更改放在一起:

git commit -m "Saving work.";
git branch saved-work;
git checkout develop;
git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force;
git add .;
git commit -m "Reset to upstream/develop";
git push --force origin develop;
git checkout branch_name;
git merge develop;

请注意,您可以使用提交哈希、其他分支名称等来代替上游/开发。使用 CLI 工具(例如 Oh My Zsh)检查您的分支是否为绿色,表明没有任何内容需要提交,并且工作目录是干净的(可以通过 git status 确认或验证)。请注意,与上游开发相比,如果提交自动添加了任何内容,例如 UML 图、许可证头等,这实际上可能会添加提交,因此在这种情况下,您可以在 origindevelopment发送到上游开发

This is what I use often:

git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force;

Note that it is good practice not to make changes to your local master/develop branch, but instead checkout to another branch for any change, with the branch name prepended by the type of change, e.g. feat/, chore/, fix/, etc. Thus you only need to pull changes, not push any changes from master. Same thing for other branches that others contribute to. So the above should only be used if you have happened to commit changes to a branch that others have committed to, and need to reset. Otherwise in future avoid pushing to a branch that others push to, instead checkout and push to the said branch via the checked out branch.

If you want to reset your local branch to the latest commit in the upstream branch, what works for me so far is:

Check your remotes, make sure your upstream and origin are what you expect, if not as expected then use git remote add upstream <insert URL>, e.g. of the original GitHub repo that you forked from, and/or git remote add origin <insert URL of the forked GitHub repo>.

git remote --verbose

git checkout develop;
git commit -m "Saving work.";
git branch saved-work;
git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force

On GitHub, you can also checkout the branch with the same name as the local one, in order to save the work there, although this isn't necessary if origin develop has the same changes as the local saved-work branch. I'm using the develop branch as an example, but it can be any existing branch name.

git add .
git commit -m "Reset to upstream/develop"
git push --force origin develop

Then if you need to merge these changes with another branch while where there are any conflicts, preserving the changes in develop, use:

git merge -s recursive -X theirs develop

While use

git merge -s recursive -X ours develop

to preserve branch_name's conflicting changes. Otherwise use a mergetool with git mergetool.

With all the changes together:

git commit -m "Saving work.";
git branch saved-work;
git checkout develop;
git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force;
git add .;
git commit -m "Reset to upstream/develop";
git push --force origin develop;
git checkout branch_name;
git merge develop;

Note that instead of upstream/develop you could use a commit hash, other branch name, etc. Use a CLI tool such as Oh My Zsh to check that your branch is green indicating that there is nothing to commit and the working directory is clean (which is confirmed or also verifiable by git status). Note that this may actually add commits compared to upstream develop if there is anything automatically added by a commit, e.g. UML diagrams, license headers, etc., so in that case, you could then pull the changes on origin develop to upstream develop, if needed.

山色无中 2024-08-16 20:50:25

只需 3 个命令即可使其工作

git fetch origin
git reset --hard origin/HEAD
git clean -f

Only 3 commands will make it work

git fetch origin
git reset --hard origin/HEAD
git clean -f
策马西风 2024-08-16 20:50:25

这里评价最高的答案没有按预期重置我的本地代码。

  1. 如今,master 通常是 main,
  2. 它不会对您可能存在的未跟踪文件执行任何操作

而是:

  1. 检查默认远程分支的名称(这不是 git 的东西,因此请在 GitHub 中检查),然后替换 main 或 master在下面的步骤 4 中这样

  2. save current stuff 替换下面步骤 4 中的 main 或 master
    git stash -u

  3. 从远程更新
    git fetch origin

  4. 重置到远程默认分支(但请参阅上面的步骤 1)
    git reset --hard origin/main

The top rated answer here did not reset my local code as expected.

  1. nowadays, master is usually main
  2. it doesn't do anything with untracked files you may have lying around

Instead:

  1. check the name of your default remote branch (this is not a git thing so check in GitHub) then replace main or master in step 4 below with this

  2. save current stuff
    git stash -u

  3. update from remote
    git fetch origin

  4. reset to remote default branch (but see step 1 above)
    git reset --hard origin/main

他不在意 2024-08-16 20:50:25

如果您想返回工作目录和索引的 HEAD 状态,那么您应该 git reset --hard HEAD,而不是 HEAD ^。 (这可能是一个错字,就像 --hard 的单破折号与双破折号一样。)

至于您关于为什么这些文件显示在已修改状态的具体问题,看起来也许您进行了软重置而不是硬重置。这将导致 HEAD 提交中更改的文件看起来就像已暂存一样,这可能就是您在这里看到的情况。

If you want to go back to the HEAD state for both the working directory and the index, then you should git reset --hard HEAD, rather than to HEAD^. (This may have been a typo, just like the single versus double dash for --hard.)

As for your specific question as to why those files appear in the status as modified, it looks like perhaps you did a soft reset instead of a hard reset. This will cause the files that were changed in the HEAD commit to appear as if they were staged, which is likely what you are seeing here.

调妓 2024-08-16 20:50:25

大量的重置和清理似乎对我本地 git 存储库中未跟踪和修改的文件没有任何影响(我尝试了上面的所有选项)。我对此的唯一解决方案是 rm 本地存储库并从远程重新克隆它。

幸运的是,我没有任何其他我关心的分支。

xkcd:Git

No amount of reset and cleaning seemed to have any effect on untracked and modified files in my local git repo (I tried all the options above). My only solution to this was to rm the local repo and re-clone it from the remote.

Fortunately I didn't have any other branches I cared about.

xkcd: Git

半窗疏影 2024-08-16 20:50:25

首先,检查 git status 是否有任何本地更改。如果是,请将它们藏起来。

然后执行:

git fetch
git reset --hard @{push}

它将当前本地分支重置为用于 git push 的同一远程分支。
当配置了 git config push.default current 时,这特别有用。例如,当您的分支是 abc 且远程分支是 origin 时,它会将其重置为 origin/abc

请参阅 Git 修订版,了解有关 <代码>@{推}

First, check with git status if you have any local changes. If yes, stash them.

Then execute:

git fetch
git reset --hard @{push}

It will reset the current local branch to the same remote branch which would be used for git push.
This is especially useful when git config push.default current is configured. For example, when your branch is abc and remote is origin, it will reset it to origin/abc.

Please see Git revisions for more details about @{push}

屋顶上的小猫咪 2024-08-16 20:50:25

我见过的唯一适用于所有情况的解决方案是删除并重新克隆。也许还有另一种方法,但显然这种方法不会让旧状态留在那里,所以我更喜欢它。如果你经常在 git 中搞砸事情,你可以将 Bash 一行设置为宏:

REPO_PATH=$(pwd) && GIT_URL=$(git config --get remote.origin.url) && cd .. && rm -rf $REPO_PATH && git clone --recursive $GIT_URL $REPO_PATH && cd $REPO_PATH

* 假设你的 .git 文件没有损坏

The only solution that works in all cases that I've seen is to delete and reclone. Maybe there's another way, but obviously this way leaves no chance of old state being left there, so I prefer it. Bash one-liner you can set as a macro if you often mess things up in git:

REPO_PATH=$(pwd) && GIT_URL=$(git config --get remote.origin.url) && cd .. && rm -rf $REPO_PATH && git clone --recursive $GIT_URL $REPO_PATH && cd $REPO_PATH

* assumes your .git files aren't corrupt

素衣风尘叹 2024-08-16 20:50:25

您是否忘记创建功能分支并错误地直接提交到 master 上?

您现在可以创建功能分支并将 master 设置回来,而不会影响工作树(本地文件系统),以避免触发构建、测试和文件锁问题:

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

Have you forgotten to create a feature-branch and have committed directly on master by mistake?

You can create the feature branch now and set master back without affecting the worktree (local filesystem) to avoid triggering builds, tests and trouble with file-locks:

git checkout -b feature-branch
git branch -f master origin/master
暗喜 2024-08-16 20:50:25
  • 抛出错误,因为它有未提交的更改。
  1. 因此,您可以使用 git stash
  • 这可以保存未提交的更改以供以后使用,然后从工作副本中恢复它们。
  • 如果您想要再次进行这些更改,可以使用 git stash apply
  1. 然后可以使用 git pull
  • 这会从远程存储库获取最近的代码。
  • Throwing error because it has uncomitted changes.
  1. So, You can use git stash
  • This saves uncomitted changes away for later use, and then reverts them from your working copy.
  • If you want these changes again, you can use git stash apply
  1. Then You can use git pull
  • This takes the recent code from remote repo.
无戏配角 2024-08-16 20:50:25
git fetch origin
git checkout main
git reset --hard origin/main
# Local `master` branch is now up to date with remote `main`
git fetch origin
git checkout main
git reset --hard origin/main
# Local `master` branch is now up to date with remote `main`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文