2

I am a bit new to Git, I think I almost have the solution but there are some things I don't like about it.

I would like to change the README.md file of a fork I follow very closely (just a few additional files that will most certainly NOT be merged upstream) so that users of my fork can have instructions for how to use my fork. To that end, I think I would like to pull from upstream while ignoring some files from upstream, such as README.md. I would also like to automate this process as I will be pulling from upstream often, and upstream could potentially change the README.md file a lot.

I've read this answer but I was not satisfied as it seems to introduce a useless merge commit.

Here is what I've come up with (untested):

# upstream remote might not be configured
UPSTREAM="https://github.com/someone/repo.git"

# .gitkeep has all the file globs I want to ignore from upstream
xargs -a .gitkeep -d'\n' git stash push

git fetch "$UPSTREAM" master
git merge --no-ff --no-commit -X theirs FETCH_HEAD
xargs -a .gitkeep -d'\n' git reset
git commit -m "Useless merge commit? :("

git stash pop

Is there a better way I can get what I want? Or can I somehow get rid of the useless merge commit?


Specifically, I would like to fork the void-packages repository, which contains build instructions for every package in the Void Linux package repositories, to add my own packages that cannot be accepted in the main repositories. I plan to use Github Actions to build the packages and distribute them, so I would like to add easily discoverable instructions on how users may add this custom package repository for themselves.

I don't want to use Nix because of #9145 (unless there is a nice way to start all GUI applications with NixGL), and I can't use Flatpak because not all of the software I want to use is available for Flatpak.

I realize my specific problem may seem a little bit stupid, but I still think it's good in general to be able to fork a project while "ignoring" some files from upstream without that useless merge commit.

fff
  • 55
  • 5
  • Might Upstream be willing to accept a PR which adds a `forks/.txt` file? And then their readme has a sentence pointing to the `forks/` directory. – Guildenstern Mar 18 '23 at 10:05
  • @Guildenstern I don't think upstream would be willing to do that. I will update the question with my specific situation so you can maybe see why. – fff Mar 18 '23 at 10:42

1 Answers1

1

I ended up using the git-filter-repo tool to do what I wanted. Here is the script:

# upstream remote might not be configured
UPSTREAM="https://github.com/someone/repo.git"

# stash so we can rebase
git stash push

git fetch "$UPSTREAM" main

# remove commits to files we don't care about between our main and upstream's main
git filter-repo --force --invert-paths --paths-from-file .gitkeep --refs main..FETCH_HEAD

# rebase so we don't have that useless merge commit!
git rebase FETCH_HEAD main

# cross our fingers that this works?
git stash pop

So far this has only been lightly tested in a dummy Git repository, so I cannot confirm how well this works in a more realistic environment. I will try this and update this answer with any success or problems I encounter.

fff
  • 55
  • 5