防止在 Git 中推送本地更改

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

Prevent local changes getting pushed in Git

gitgit-push

提问by Gon?alo Marrafa

I have cloned a repository and the masterbranch in my repo is tracking origin/master. I created a workbranch and changed some config files specific to my dev machine to make the app work.

我已经克隆了一个存储库,并且我的存储库中的master分支正在跟踪origin/master. 我创建了一个work分支并更改了一些特定于我的开发机器的配置文件以使应用程序正常工作。

My normal workflow will be to switch to masterbranch, merge changes made in workbranch and pushthose changes upstream. The problem is that i don't want my specific chnages to get pushed. When i merge my workbranch into masterthose changes are merged also.

我的正常工作流程是切换到master分支,合并work分支中所做的push更改和上游的更改。问题是我不希望我的特定更改被推送。当我将我的work分支合并到master这些更改中时,也会合并。

The only solution I've found so far is not to commit those changes in workbut that's not a satisfactory solution.

到目前为止,我找到的唯一解决方案是不提交这些更改,work但这不是一个令人满意的解决方案。

回答by Mauricio Scheffer

If you want to prevent committing (therefore also pushing) these local config files, you could use git update-index --assume-unchanged. Files marked with this flag will be assumed to never change (until you reset the flag with --no-assume-unchanged)

如果你想阻止提交(因此也推送)这些本地配置文件,你可以使用git update-index --assume-unchanged。标有此标志的文件将被假定为永远不会更改(直到您使用 --no-assume-unchanged 重置该标志)

回答by Waleed Al-Balooshi

If the files are not already being tracked, you can add them to a .gitignore file to your local repository, otherwise you will need to use git update-index --assume-unchangedlike Mauricio said.

如果文件尚未被跟踪,您可以将它们添加到本地存储库的 .gitignore 文件中,否则您将需要git update-index --assume-unchanged像 Mauricio 所说的那样使用。

Following is more information about .gitignore files:

以下是有关 .gitignore 文件的更多信息:

http://git-scm.com/docs/gitignore

http://git-scm.com/docs/gitignore

回答by Greg Hewgill

One way to solve this is to "cherry-pick" each change from your work branch into master before you push upstream. I've used a technique where I include a word like NOCOMMIT into the commit message for local changes only, and then use a shell script something like this:

解决此问题的一种方法是在向上游推送之前将每个更改从工作分支“挑选”到主分支。我使用了一种技术,我将像 NOCOMMIT 这样的词包含在仅用于本地更改的提交消息中,然后使用如下所示的 shell 脚本:

#!/bin/sh

BRANCH=`git branch | grep ^\* | cut -d' ' -f2`
if [ $BRANCH != "master" ]; then
  echo "
(Assume you use linux)
vi .gitignore
//Add all your config file inside

//run the following command to remove your config file from GIT cache if your config files is already track by GIT

git rm --cached myconfig.php
: Current branch is not master" exit 1 fi git log --pretty=oneline work...master | grep -v -E '(NOCOMMIT|DEBUG):' | cut -d' ' -f1 | tac | xargs -l git cherry-pick

This cherry-picks each change that is notmarked with NOCOMMIT (or DEBUG) into the master branch.

这会将标记为 NOCOMMIT(或 DEBUG)的每个更改挑选到主分支中。

回答by Finau

What you need is a .gitignore file and add all your config file inside the .gitignore file.

您需要的是一个 .gitignore 文件,并将所有配置文件添加到 .gitignore 文件中。

Once your .gitignore file is ready you might need to removed your config files from the Cache

一旦您的 .gitignore 文件准备就绪,您可能需要从缓存中删除您的配置文件

Here's the command:

这是命令:

##代码##