2

I'm trying to learn git with a dummy project and I'm facing a problem. I was trying to delete a file from my repo so it doesn't show up on my github repo page. So I deleted the file on the master branch and committed the change. I then discovered a problem and wanted to go back. I checked out my commit in a new branch by git checkout -b <branch-name> <commit hash>. So far so good, I even got the file that I deleted back in my folders. Now I just need to merge this with my master branch so that has the deleted file too. However when I do git checkout master and then git merge <branch-name>. It just says that the branch is already up to date even though I clearly don't see the deleted file when I'm in the master branch. I tried committing changes while on this newly created branch, as stated in a comment below, however this simply says that the branch is already up to date and the working tree is clear.

I'm a bit confused about these committing and probably did something wrong. What is the issue and how do I solve it and avoid it in the future?

Cecilya
  • 519
  • 1
  • 5
  • 20
Chase
  • 5,315
  • 2
  • 15
  • 41
  • On which branch did you do the original deletion? You should edit your question and tell us the _exact_ steps you took, paying careful attention to tell us on which branch you were when you did what. – Tim Biegeleisen Nov 06 '18 at 12:31
  • And did you commit the file that you recovered on your new branch? As merging the new branch without any new commit would not have any changes – Ange Nov 06 '18 at 12:32
  • @Ange Included more information as suggested. Sorry for not doing it initially. – Chase Nov 06 '18 at 15:04
  • @TimBiegeleisen Edited it to include more info. – Chase Nov 06 '18 at 15:05
  • 3
    It sounds like you're trying to merge a commit into master that is already a direct ancestor, in these cases, there is nothing to merge because the commit is already on the master branch. Git thinks in terms of commits and ancestry. Branches are just labels that make it easier for humans to conceptualize the relationship between commits. – LightBender Nov 06 '18 at 15:48

2 Answers2

0

Creating a branch from an older commit wouldn't be enough to merge it back, considering said older commit is already part of master history.

If you want to get the file from an older commit, one simple workaround would be to:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

As mentioned in one of the comments, Git is unable to see any difference in the two branches as the commit already exists in the master branch.

Seems like your use case is that of reverting a commit that you have previously done on master branch. So you should be able to do the following:

  • git checkout master
  • Find the commit id of the commit in which the file was deleted. (use git log for the same)
  • git revert <commit_id>

git revert will prepare a new revert commit removing all the changes done in that commit_id and this commit can be directly pushed or merged into master branch.

face
  • 1,503
  • 13
  • 27