Git 忽略本地文件更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24983762/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:21:34  来源:igfitidea点击:

Git ignore local file changes

git

提问by user2395365

I've tried both

我都试过了

git update-index --assume-unchanged config/myconfig

and

editing .git/info/excludeand adding config/myconfig

编辑.git/info/exclude和添加config/myconfig

however when I do git pull I always get:

但是,当我执行 git pull 时,我总是得到:

Updating 0156abc..1cfd6a5 error: Your local changes to the following files would be overwritten by merge: config/myconfig Please, commit your changes or stash them before you can merge. Aborting

更新 0156abc..1cfd6a5 错误:您对以下文件的本地更改将被合并覆盖:config/myconfig 请在合并之前提交您的更改或隐藏它们。中止

What am I missing?

我错过了什么?

回答by Nick McCurdy

git pullwants you to either remove or save your current work so that the merge it triggers doesn't cause conflicts with your uncommitted work. Note that you should only need to remove/save untracked files if the changes you're pulling create files in the same locations as your local uncommitted files.

git pull希望您删除或保存您当前的工作,以便它触发的合并不会与您未提交的工作发生冲突。请注意,如果您正在拉取的更改在与本地未提交文件相同的位置创建文件,您应该只需要删除/保存未跟踪的文件。

Remove your uncommitted changes

删除未提交的更改

Tracked files

跟踪文件

git checkout -f

Untracked files

未跟踪的文件

git clean -fd

Save your changes for later

保存您的更改以备后用

Tracked files

跟踪文件

git stash

Tracked files and untracked files

跟踪文件和未跟踪文件

git stash -u

Reapply your latest stash after git pull:

git pull以下时间后重新应用您的最新存储:

git stash pop

回答by Eric

You most likely had the files staged.

您很可能已暂存了文件。

git add src/file/to/ignore

To undo the staged files,

要撤消暂存文件,

git reset HEAD

This will unstage the files allowing for the following git command to execute successfully.

这将取消暂存文件,允许以下 git 命令成功执行。

git update-index --assume-unchanged src/file/to/ignore

回答by user3858653

You probably need to do a git stashbefore you git pull, this is because it is reading your old config file. So do:

你可能需要git stash在你之前做一个git pull,这是因为它正在读取你的旧配置文件。所以这样做:

git stash
git pull
git commit -am <"say first commit">
git push

Also see git-stash(1) Manual Page.

另请参阅git-stash(1) 手册页

回答by rashok

If you dont want your local changes, then do below command to ignore(delete permanently) the local changes.

如果您不想要本地更改,请执行以下命令以忽略(永久删除)本地更改。

  • If its unstaged changes, then do checkout (git checkout <filename>or git checkout -- .)
  • If its staged changes, then first do reset (git reset <filename>or git reset) and then do checkout (git checkout <filename>or git checkout -- .)
  • If it is untracted files/folders (newly created), then do clean (git clean -fd)
  • 如果它的未暂存更改,则执行 checkout (git checkout <filename>git checkout -- .)
  • 如果它的分阶段发生变化,那么首先执行 reset (git reset <filename>git reset) 然后执行 checkout (git checkout <filename>git checkout -- .)
  • 如果是未压缩的文件/文件夹(新创建的),则执行 clean ( git clean -fd)

If you dont want to loose your local changes, then stash it and do pull or rebase. Later merge your changes from stash.

如果您不想丢失本地更改,请将其藏起来并进行拉取或变基。稍后从 stash 合并您的更改。

  • Do git stash, and then get latest changes from repo git pull orign masteror git rebase origin/master, and then merge your changes from stash git stash pop stash@{0}
  • 执行git stash,然后从 repogit pull orign master或获取最新更改git rebase origin/master,然后从 stash 合并更改git stash pop stash@{0}