git include changes from previous commits

76 views Asked by At

I have a master branch. On some t day I merged new files to the master, but realised that these files caused some bugs. I created a new branch (from master), in this branch, I deleted all the changes of the previous commit and committed it to master. Things go as usual, and there has been few more unrelated commits to master. Today, I realised that the issue was not caused by all the files, but only a small bug fix. So, I am trying to bring back all those files (along with bugfix), but unable to do so. I was thinking if I create a new branch from the point when I committed those files originally, I would get back the files, but it just shows up as a blank branch (no changes) when I try to merge to master?

I am sorry, I am not that familiar with git here. My most of the use has been limited to just git pull and git push. Can someone please help me understand what I am doing wrong?

2

There are 2 answers

0
eftshift0 On BEST ANSWER

So... suppose you have this:

* GGG Last commit in main branch (main)
* FFF Merging feature1 in main branch
|\
* | EEE Third commit in main branch
| * DDD another commit in a feature branch (feature1)
| * CCC Some commit in a feature branch
|/
* BBB second commit
* AAA first commit

So, what you can do is, first:

git checkout -b blah $( git merge-base FFF^ FFF^2 ) # go back to the commit where the 2 branches diverged

With that, you get:

* GGG Last commit in main branch (main)
* FFF Merging feature1 in main branch
|\
* | EEE Third commit in main branch
| * DDD another commit in a feature branch (feature1)
| * CCC Some commit in a feature branch
|/
* BBB second commit (HEAD -> blah)
* AAA first commit

Now, let's cherry-pick the original commits of feature1:

git cherry-pick HEAD..feature1

Now, you have this (not ordered chronologically, anyway):

* GGG Last commit in main branch (main)
* FFF Merging feature1 in main branch
|\
* | EEE Third commit in main branch
| * DDD another commit in feature branch (feature1)
| * CCC Some commit in feature branch
|/
|
| * DDD' another commit in a feature branch (HEAD -> blah)
| * CCC' Some commit in a feature branch
|/
* BBB second commit (HEAD -> main)
* AAA first commit

And to git, blah has not been merged into main even though it is equivalent to feature1. If you merge blah, it should be fine.

0
Caleb On

Follow These Steps to Retrieve Your Missing Files

We are going to go back to the previous commit, so if you have any uncommitted changes at the moment, please either commit them or (if you're not ready to commit) stash them. I explain stashing below.

If You're Stashing Them

Run git add -A to stage any current changes in the working directory, so if you added new files, they are saved before you stash. Then, run git stash.

After You've Either Committed or Stashed

  • Run git log --oneline --all --graph --reflog. This will show you a full view of your blobs, branches, and history.
    • Use the arrow keys to navigate, if needed. Press the q key to exit this view.
  • Locate the blob that has HEAD next to it. This is your current position. There should be 7-character code before it. This is the ID of your blob. Note this. Make sure you use your actual blob ID and not the one in the example picture that I am showing you (do not use "1A2B123") Example of where this blob ID is located (in this case 1A2B123)
  • Locate the blob that has the version of the files that you need. Note this blob ID as well.
  • Use git reset --hard <blobId> to go back to the version with the files.
  • Copy the files to wherever you want, such as your Desktop.
  • Use git reset --hard <blobId> to go back to your most recent version.
  • If you stashed earlier, then run git stash pop to retrieve those uncommitted changes from before.

You can now copy your files back into your project and commit them/push them if you want.