GIT: move back to an old commit and merge that with master

Given a project with 2 files: fileA and fileB. Both were once added to a git-repository. Then I added fileA to .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?

  • How to change the main branch in BitBucket?
  • Best way to integrate Git with Ant?
  • (git) diff output relative path?
  • Error when pushing to master
  • Move .git into a different directory than the source it manages
  • How to work with (sub)directory renames in Git?
  • Sorry for the title. I was trying to be descriptive, but ..

  • Why does gitk have no copy/paste menu item under Edit?
  • git history of a source line
  • Is it possible to disable do “push force”, which overwrite “master” trunk of repository?
  • Can I have git gui and git bash and eclipse git plugin looking at same repo simultaneously?
  • how to get a list of merged files in git?
  • Pushing to Git from webstorm
  • 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:

    $ 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 fileA anymore.

    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.

    Git Baby is a git and github fan, let's start git clone.