- Ran
git log
, which showed following
commit on master( in my case, it is develop branch) a867b4af366350be2e7c21b8de9cc6504678a61b`
Author: Me <[email protected]>
Date: Thu Nov 4 18:59:41 2010 -0400
blah blah blah...
commit 25eee4caef46ae64aa08e8ab3f988bc917ee1ce4
Author: Me <[email protected]>
Date: Thu Nov 4 05:13:39 2010 -0400
more blah blah blah...
commit 0766c053c0ea2035e90f504928f8df3c9363b8bd
Author: Me <[email protected]>
Date: Thu Nov 4 00:55:06 2010 -0400
And yet more blah blah...
commit 0d1d7fc32e5a947fbd92ee598033d85bfc445a50
Author: Me <[email protected]>
Date: Wed Nov 3 23:56:08 2010 -0400
Yep, more blah blah.
As I'm not able to revert or reset my master branch to certain commit, created a new branch from the old commit, which contains the deleted branch by running following command
git checkout -b "branch_name" 0d1d7fc32e5a947fbd92ee598033d85bfc445a50
USING git hash of 03 November 2010, created a new branch (this commit contains the xyz.java file which does not exist in the latest commit on master (develop) branch)
- Do not have any more changes to make in this new branch (created in step 2 above). purpose of creating this new branch was to revert the xyz.java file, which has been deleted in master. Pushed this new branch to remote.
Git does not allow me to create a PR for this new branch as it shows an error that this new branch does not contain any change, which is not already contained in develop.
If I rebase my new branch with master (develop, in my case) by running following command, it deletes xyz.java file from my new branch.
git pull origin develop
How do you get xyz.java file back in my new branch, which allows git to recognise this new branch contains xyz.java file, which has been deleted in master branch?
CodePudding user response:
Find the last commit before deleting this file. Remember the hash. Then checkout this file to your current branch.
git checkout <hash> -- <full/path/to/the/file>
For example:
git checkout 0d1d7fc32e5a947fbd92ee598033d85bfc445a50 -- xyz.java
Then make a commit.
CodePudding user response:
TL;DR
You need to create a PR from a branch that starts from the latest state of master
and adds the file back in, not from a old branch.
My solution
The fact that you don't have permission to push to master does not mean you cannot add commits to master. It just means you have to use a PR to do so.
Steps to follow:
- Create a new branch from
master
in its latest state. - On that branch add and commit the file back in (e.g., using @Ivan's answer, or by using
git revert
on the commit where you deleted it). - Push that branch to the remote.
- Submit a PR from it, and that will add the file back into
master
once it's approved and merged.
The point of having push protection on master
is not to prevent you from adding commits onto master, but rather to force you to do it through a PR, typically for the purpose of having a review on it before it gets merged.
Why your approach didn't work
In the attempt you describe in the question, you started back from the old branch instead of starting from the latest version of master. The problem with that is that the master branch continues to have a commit that deletes that file, so merging that old branch in again, Git continue to (correctly) say, well, I'm merge a branch with the file into a branch that says delete that file, so that file should stay deleted.
In contrast, a new branch that re-adds is, when merged into main, will re-add it since you're merging one branch that doesn't have it and one branch that's adding it.