GIT: move back to an old commit and merge that with master
Given a project with 2 files:
fileB. Both were once added to a git-repository. Then I added
.gitignore (and flushed the git-cache), so that in the current master only
fileB is present. I would like to clone this repository to another machine now, but fileA is missing.
The next thing I did was to create a branch from the commit where both files were present. The big question now is: how do I get back to the master commit without losing that file again?
Sorry for the title. I was trying to be descriptive, but ..
2 Solutions collect form web for “GIT: move back to an old commit and merge that with master”
If all you need to do is restore one file, you can do that with
$ git checkout <revision_where_fileA_exists> fileA $ git add fileA $ git commit -m "Restored fileA"
You will also have to edit your
.gitignore so that it isn’t ignoring
I think Josh’s answer is probably what you want for this situation, but if you run into again, you have a couple options… The classic merge or pull will always work, you just need to specify -f if you are trying to undelete files that are in your ancestry. Otherwise, git will complain.
$ git checkout master $ git pull -f . <branch>:master
That command should auto-commit your change if you just had a file deletion. You can also explore the following comands for more revert specific features:
$ git reset $ git revert
Reset is really used if there is a small change that you want to back out that doesn’t need to pollute the logs, but it is quite powerfull, look into the docs.
Revert is for undoing things that have happened in the past. More similar to “backing out” a change.