Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent losing .gitignore when checking-out older branches?

Tags:

git

If I am in master and stuff I want safe from git is ignored, I then:

    git checkout <oldbranch or commit>

But the old branch doesn't have the ignored files arrgh! So now the untracked files could get added etc and are vulnerable to reset --hard etc.

Is there more elegant fix here other than:

    git checkout master .gitignore
    #maybe write a hook to do this every checkout of <oldbranch or commit>?

Or constantly manage copying .gitignore into .git/info/exclude?

like image 630
sabgenton Avatar asked Nov 18 '25 08:11

sabgenton


1 Answers

Adding rules in .git/info/exclude is one way, but it remains a local workaround: it won't get pushed like a .gitignore file would.

The best option remains to create the .gitignore in the checked out branch:

git checkout aBranchWithoutGitignore
git checkout master -- .gitignore
git add .gitignore
git commit -m "Add .gitignore from master"

Then cherry-pick that commit to other branches.

like image 97
VonC Avatar answered Nov 20 '25 15:11

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!