简体   繁体   English

Git提交保持本地

[英]Git commits keep local

I'm wondering if it is possible to make a commit for some changes that if I push, these commits do not get pushed with the other commits to the remote branch. 我想知道是否有可能对某些更改进行提交,如果我进行推送,这些提交不会与其他提交一起推送到远程分支。

for example: I've got some local changes i need for testing purposes. 例如:为了测试目的,我需要一些本地更改。 Like some changes to the config files and some json files. 就像对配置文件和一些json文件进行了一些更改。 These changes will only be needed on this local branch and should never get on the remote branch. 这些更改仅在此本地分支上需要,而永远不会在远程分支上。 So when I create a commit I'm always trying to be carefull not to stage those exact files and every time I switch branches I need to stash and pop these changes. 因此,当我创建提交时,我总是要小心谨慎,不要暂存那些确切的文件,并且每次切换分支时,我都需要隐藏并弹出这些更改。

So what I'm wondering is if there is a way within Git to keep those changes on the local branch without having to stash/pop or unstage them. 所以我想知道的是,Git中是否有一种方法可以将这些更改保留在本地分支上,而不必隐藏/弹出或取消暂存它们。

There are multiple ways to handle this but it depends on the extent of your local modifications. 有多种处理方法,但这取决于您本地修改的程度。

Let's first assume you have some configuration files in your repository. 首先,假设您的存储库中有一些配置文件。 These configuration files may contain things such as the local hostname, connection strings or similar for databases, etc. 这些配置文件可能包含诸如本地主机名,连接字符串或数据库类似名称之类的内容。

In this situation the best way is to only commit template configuration files. 在这种情况下,最好的方法是仅提交模板配置文件。

For instance, if the application needs a file named app.config to be present, but this file contains such things, you would instead commit a template file named app.config.template . 例如,如果应用程序需要存在一个名为app.config的文件,但是该文件包含此类内容,则您可以提交一个名为app.config.template的模板文件。 This file would not contain actual hostnames, usernames, etc. but at most contain placeholders where you're expected to add this information. 该文件将不包含实际的主机名,用户名等,但最多包含您希望在其中添加此信息的占位符。

Then, in your build script (or similar) you would have something that would look like this: 然后,在您的构建脚本(或类似脚本)中,您将具有如下所示的内容:

IF NOT EXIST app.config COPY app.config.template app.config

This would, if needed, make a fresh copy from the template to the real file, on build. 如果需要,这将在构建时从模板到实际文件进行全新复制。

Additionally, you would instruct git to ignore the real file: 另外,您将指示git忽略实际文件:

# in .gitignore
app.config

Now, sometimes this is not enough. 现在,有时这还不够。 It should be, but sometimes it isn't. 应该,但是有时不是。

In this situation a different approach would use an extra branch for this. 在这种情况下,另一种方法将为此使用额外的分支。 Let's assume you're developing on a branch named develop . 假设您正在一个名为develop的分支上进行develop

You would then first create a new branch for your local only modifications: 然后,您将首先为本地修改创建一个新分支:

git branch develop-local develop

Then you would check out this branch, make all the necessary local modifications, and commit these: 然后,您将签出该分支,进行所有必要的本地修改,然后提交以下内容:

git checkout develop-local
# hack hack hack
git add .
git commit -m "LOCAL modifications for test environment"

Then you would switch back to the real branch and work on a case: 然后,您将切换回真实分支并处理案例:

git checkout develop
# work work work
git add .
git commit -m "Case #123, fixed bug in SQL parsing"

To test this, go back to your local branch, merge in the real branch and test: 要对此进行测试,请返回您的本地分支,合并到实际分支中并进行测试:

git checkout develop-local
git merge develop
# test test test

Finally: 最后:

  • never push develop-local , only develop 从不推动develop-local develop ,仅develop
  • never merge develop-local back into develop 永远不要develop-local develop合并到develop

Yes, but it's a little cumbersome. 是的,但是有点麻烦。

Read 7.6 Git Tools - Rewriting History from the docs. 阅读7.6 Git工具-从文档重写历史记录

I usually configure my projects to include files like main.config and then local.config if only it is exist and then merge them in a propper way. 我通常将项目配置为包含main.config之类的文件,然后包含local.config(如果仅存在),然后以适当的方式合并它们。 And those local files are .gitignore 'd of course. 这些本地文件当然是.gitignore

Depends on your architecture, the article on .git/info/exclude can be handy 根据您的架构,有关.git / info / exclude的文章可能会很方便

Consider using assume unchanged: 考虑使用假设不变:

When this flag is specified, the object names recorded for the paths are not updated. 指定此标志时,为路径记录的对象名称不会更新。

git-update-index from the docs. 来自文档的git-update-index

git update-index --assume-unchanged app.config

That would stop app.config from being marked as changed. 这将阻止app.config被标记为已更改。

' temporarily ignoring files ' is probably also worth a read. 暂时忽略文件 ”可能也值得一读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM