Github: How to Delete/ Reverse Folders and Files in a Branch without Actually Deleting Them From Repository

haRies Efrika
4 min readMar 2, 2023

--

https://pict.sindonews.net/dyn/620/pena/news/2022/08/02/207/844365/8-istilah-umum-dalam-platform-github-pemula-wajib-tahu-xsd.jpg
ref https://pict.sindonews.net/dyn/620/pena/news/2022/08/02/207/844365/8-istilah-umum-dalam-platform-github-pemula-wajib-tahu-xsd.jpg

This is based on real use case. You have been working on private branch where soon you will create PR to merge with master. Then you found out, a folder with many of its file contents are actually not needed to be part of your PR, and want to rely on latest one from the master. Things can get dirty if you don’t know how to deal with it. The reason why I came up with this article is because I tried to google for best solution and it took me quite a while. I hope this summarizes the solution for you.

Let’s say you and your colleague are working in a same repository, with target main branch is master. For example purpose, these are the tracked files/ folders in master.

  • account (folder)
  • statistics (folder)

Your work is concentrated in statistics folder. While your colleague is working and focusing on account folder for different project. While working on statistics, you find out that you require library or probably RPC files from the account for your project. Therefore you’re helping developing and editing some files belong to account.

  • account/api/rpc/user.pb.go
  • account/internal/impl/user.go

Say those are two files you edited for account.

Time flies, you have added a lot of commits mainly for your statistics folder. But meanwhile, your colleague have submitted and merged his PR to master. And you know what, even he is being so kind to include the required changes in user.pb.go and user.go that are in your branch.

So basically right now, the account folder in master has become the up-to-date source and you should not replace again those two files with your branch PR later on. Reasons:

  • It is a waste of action/ not necessary. Ideally you should have a PR that does not include changes in account at all.
  • If you force to still include your files in account, you may risk a merge conflict and risk creating new issues when merging it.

So what can we do?

PS: We may not use git rm since it is basically proposing to remove the original files in master, when we merge our branch.

Alternative Solution #1

We could try to copy the up-to-date user*.go files from latest master into your branch and then create a PR. This for sure will avoid merge conflict. But it may be tricky if you actually have a lot of files under account to synchronize. And going back to our previous reason, this is a waste of action.

Alternative Solution #2

We could create a fresh branch from master, and then just copy your work from statistics folder, then create a PR. This could work, but you actually will lose precious commit history you had while developing the statistics.

Final Solution

Files or folders, in your working branch, can actually be reverted into previous commit state. We will utilize this.

We will search from git log or from github website, what was the commit hash in folder account before we added our commits into it.

Example from github website:

Say that dd2d4dc... was the latest commit before our addition to the account folder. You can also re-verify this from the git log history. Copy that SHA1 into clipboard.

What we need to do is only to run this command:

git checkout <commit ID SHA1> -- <file or folder>
git checkout dd2d4dcb208119eff53431329ad23e6cb363f930 -- account

The double dash -- is not bash syntax, but a parameter explicitly required by git to specify that what follows, i.e. account, is actually referring to files or folders, and not branch.

After running that command, we will find out using git status that the files under account are now in modified state. Next we only need to commit it.

git commit -m "take out folder account from this branch PR"

Wait — we still need another commit?

YES. But this commit is special, it does not propose a change for working tree. Instead it actually will exclude the folder from our working branch.

We can verify what are the files in our branch that later will be used to compare during PR, via this command:

git diff --name-only <manually checked earliest commit hash> HEAD
git diff --name-only a6e12c50d5996ba509ed8998fc0409f0d5af20c2 HEAD

For the first hash parameter, we need to check out git log, what was the latest commit before we made our modification in our branch. That will summarize all files changed so far. This command is also useful for us to verify it locally first. People usually rely on the PR comparison page on github web to see what files we are submitting 😜

Credits to https://www.linkedin.com/in/yovi02/ who has helped me finding this solution.

Thanks for reading. Cheers 🍻

--

--

No responses yet