1

I am using git using and want to get code from previous commits to the head. I have figured out a way to do this but that seems more like a hack, I am wondering if there is a better way of doing it.

The method I used is as follows: I involve going to that commit, branching, making changes to desired files, committing it, checking out to master, and mering it the new branch created.

  1. checkout to
  2. creating a branch
  3. making inconsequential changes to the files
  4. committing them
  5. checking out master
  6. merging the new branch with master
Zarak Khan
  • 21
  • 4
  • 1
    I suspect that what you really want here is to perhaps compare the code for some current file against an earlier version and then maybe bring in some of that older code. IMHO the best way to do this is to use the merge tool from your IDE's Git plugin. – Tim Biegeleisen Sep 13 '20 at 09:31
  • Do you want to restore a file at a certain revision or do you want to re-apply the changes from that commit? To me it is not really clear what you are trying to achieve. Could you maybe add an example content of files, before and after with your expecations of the final result? – knittl Sep 13 '20 at 10:01
  • 1
    I'm asking, because you will only get your new changes into master, not any "old" changes (they were already merged to master, weren't they?) – knittl Sep 13 '20 at 10:53

1 Answers1

0

NOTE: Requests for clarification were not answered in the comments, so I'm just taking a stab here.

If you want to restore a file from a previous commit, i.e. revert the content of a file to a former state, you would simply execute the checkout subcommand:

git checkout hash_of_old_commit -- path/to/file

The file will automatically be staged. To permanently record this state in your Git commit history, create a new commit:

git commit -m 'Revert file xyz to commit_hash'
knittl
  • 246,190
  • 53
  • 318
  • 364