0

I'm a beginner with Git. So here's the scenario, In an empty text file, I added One String and performed the following command:

git init
git add sample.txt
git commit -m "Added One"
git push origin master

Next, I added Two on the next line and performed the following command:

git add sample.txt
git commit -m "Added Two"
git push origin master

And ended up until Three making it contain:

One
Two
Three

Then I performed git reset --hard 'value of the first commit here' to bring back the file which contains One and added Four making it:

One
Four

So when I performed a commit and push, the following error showed:

Updates were rejected because the tip of your current branch is behind its remote counterpart.

How can I push my commit rejecting the other 2 commits so that the final text file will contain:

One
Four
ajdeguzman
  • 1,223
  • 3
  • 16
  • 27

1 Answers1

3

You need to "force push", to forcibly replace the branch on origin with your local branch:

git push -f
user229044
  • 232,980
  • 40
  • 330
  • 338
  • 2
    Is there any consequences in using this command? – ajdeguzman Sep 08 '15 at 02:26
  • You will obviously lose the commits you overwrite, and anybody sharing your repository will have to reset their branch to the same state as yours. – user229044 Sep 08 '15 at 02:27
  • Is there any command that will do this as well as retained the commits in log. I performed `git log` but the 2 commits where deleted in logs. – ajdeguzman Sep 08 '15 at 02:28
  • @ajdeguzman No, that makes no sense. You cannot both retain the commits in your branch and *not* retain the commits in your branch. If you want to keep a reference to those commits, check out a new branch. – user229044 Sep 08 '15 at 02:29