Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove all local commits and go to the last commit on the branch master (on remote repository)? [duplicate]

Tags:

git

github

I have made some changes in the repository and registered a commit (on local). And then I executed this:

$ git pull origin master

And my local commit merged with the pulled one. Now I regret. I want to get the last version which is on the remote branch master and get ride of the local commit (which is merged).

I can change the working directory by this command:

// the last commit on the remove branch master
$ git checkout 84acc42 .

But if I make some changes on it and push it, I guess that previous changes still are exists and will be pushed as a commit too. Because that commit is exists.

Noted that $ git reset --hard doesn't remove anything. I executed that and still see the changes I don't want.

Any idea how can I get the last version which is on the remote repository (branch master) and remove everything else? I can do that by removing the local working directory and cloning it again from the repository. But I want to know if there is another way.

like image 997
Martin AJ Avatar asked Apr 18 '18 17:04

Martin AJ


1 Answers

Fetch the origin/master.

git fetch --all

Stage all the changes. So that, No untracked files will be left behind.

git add .

Then, run reset command,

git reset origin/master --hard

This will throw away all the changes made in the branch, will be exact copy of origin/master.

like image 189
Abdullah Al Maruf - Tuhin Avatar answered Nov 15 '22 09:11

Abdullah Al Maruf - Tuhin