Git:我怎么能修改文件并仍然检查其他分支?

时间:2022-03-04 05:03:39

I had to modify a few files in a project, but they are configuration files and I don't want to commit them (the config is just for my environment). However, I'd still like to change branches, but when I use git checkout it says that my local changes would be overwritten. I don't know how to handle this.

我不得不修改项目中的一些文件,但它们是配置文件,我不想提交它们(配置只适用于我的环境)。但是,我仍然想更改分支,但是当我使用git checkout时,它表示我的本地更改将被覆盖。我不知道如何处理这个问题。

I would like to have these changes, don't want to commit them (cause I then send pull requests on github and those changes would appear there, which is not what I want), but I'd still like to change branches. Is it possible?

我想进行这些更改,不想提交它们(因为我然后在github上发送pull请求,那些更改会出现在那里,这不是我想要的),但我仍然想更改分支。可能吗?

3 个解决方案

#1


3  

  • When the files are untracked, put them in .gitignore so you never commit them.

    当文件未跟踪时,将它们放在.gitignore中,以便您永远不会提交它们。

  • When the files are tracked, stash them:

    跟踪文件时,将其存储:

    git stash
    git checkout <other branch>
    git stash apply  #(This may or may not give some conflicts)
    

    When you no longer need these changes

    当您不再需要这些更改时

    git stash drop
    

    Note that you can apply and drop in the same command with git stash pop, but this does not drop if there are conflicts, so its sometimes better to do them separately to avoid confusion.

    请注意,您可以使用git stash pop应用和删除相同的命令,但如果存在冲突,则不会丢失,因此有时最好单独执行以避免混淆。

#2


0  

Untrack the files from Git and put the files in the .gitignore list.

从Git中取消文件并将文件放在.gitignore列表中。

git rm --cached filename

git rm --cached filename

#3


0  

If your modifications aren't supposed to ever leave your repo, use filters to apply and strip your local changes. Here's a worked single-file case.

如果您的修改不应该留下您的仓库,请使用过滤器来应用和删除您的本地更改。这是一个有效的单文件案例。

#1


3  

  • When the files are untracked, put them in .gitignore so you never commit them.

    当文件未跟踪时,将它们放在.gitignore中,以便您永远不会提交它们。

  • When the files are tracked, stash them:

    跟踪文件时,将其存储:

    git stash
    git checkout <other branch>
    git stash apply  #(This may or may not give some conflicts)
    

    When you no longer need these changes

    当您不再需要这些更改时

    git stash drop
    

    Note that you can apply and drop in the same command with git stash pop, but this does not drop if there are conflicts, so its sometimes better to do them separately to avoid confusion.

    请注意,您可以使用git stash pop应用和删除相同的命令,但如果存在冲突,则不会丢失,因此有时最好单独执行以避免混淆。

#2


0  

Untrack the files from Git and put the files in the .gitignore list.

从Git中取消文件并将文件放在.gitignore列表中。

git rm --cached filename

git rm --cached filename

#3


0  

If your modifications aren't supposed to ever leave your repo, use filters to apply and strip your local changes. Here's a worked single-file case.

如果您的修改不应该留下您的仓库,请使用过滤器来应用和删除您的本地更改。这是一个有效的单文件案例。