0

I am a newbie in git and currently trying out various combinations to understand git.

I cloned my repository which had two files. Now i made changes to one file (let's say "first.txt") and committed it to staging.

After this step i wanted to reset the "first.txt" to its remote counterpart such that no git commit history related to "first.txt" was present for my branch. Now if I use -

git checkout origin/master first.txt

This will just replace my file with the remote file but git commit history for that file was not be replaced.

If i use -

git reset --hard <sha1 commit id>

then it restores the branch to that particular commit and all commits made there after are lost.

Now what i wanted to understand was if there is any method by which we can hard reset a file such that its entire content and commit history is restored to its remote's content and history.

Thanks for taking out time to read my question !

If downvoting then kindly let me know what else could have been improved in the question.

Thanks

harsrawa
  • 422
  • 5
  • 18
  • http://stackoverflow.com/questions/215718/reset-or-revert-a-specific-file-to-a-specific-revision-using-git – Salailah Sep 19 '16 at 20:26
  • The method mentioned in the link above is useful only to restore the content of the file to a specific commit. The first command that i mentioned was the same one. The problem with this method is that the commit history of that file does not change in local. – harsrawa Sep 19 '16 at 20:58

1 Answers1

3

Commits represent changes of entire repository. If you want to work with restoring single file to its some other version checking out and commiting is the best option. Otherwise you have to restore entire repo to its remote like this:

git reset --hard origin/master
git pull origin master
Ayon Nahiyan
  • 2,140
  • 15
  • 23
  • But this will reset all the local commits made in other files as well. Can we not just restore a single file from repo including its history ? – harsrawa Sep 19 '16 at 21:09
  • I understand that there can be a method of checking out that file from remote and then committing it. But it will then create unnecessary commits which are now invalid. – harsrawa Sep 19 '16 at 21:10
  • 1
    I dont think thats possible in git. Commits are history of entire repository. You cant just restore one files commit history and not effect others. Checking out one files remote version and then committing it is the best option here. – Ayon Nahiyan Sep 19 '16 at 21:11
  • Yes i think you are right. However would it not be better if we are able to restore a single file instead of complete repo ? This way those past commits which are not valid anymore would be removed. Thus allowing for a clear history. – harsrawa Sep 19 '16 at 21:14
  • :) Thats why the best practice here is to create your own branch. Then commit as much as you want in your branch. Then when you are sure that you are ready make a squash merge into master. Then delete the other branch. You will have only 1 commit for each branch into master using squash – Ayon Nahiyan Sep 19 '16 at 21:23
  • @AyonNahiyan also this is why best practice is to commit in small logical chunks, usually just one file per commit, so when you need to revert to a particular commit, then it usually only effects one file. – Jeff Puckett Sep 19 '16 at 22:55