giovaZ
giovaZ

Reputation: 1470

Override local files with the ones of a specific branch

I have a git server with some branches:

    $ git log
commit 03a870730a0273f354236b4d724c4f3379b6d182
Author: Giovanni Antonioni <[email protected]>
Date:   Fri Jun 5 13:31:55 2015 +0200

    Description 1

commit 4435b7718609c3622542b6b2a8d7a59994083044
Author: Giovanni Antonioni <[email protected]>
Date:   Fri Jun 5 10:30:59 2015 +0200

    Description 2

commit 8a8bafdd6e0879b5ef726db13e786caffc65fb13
Author: Giovanni Antonioni <[email protected]>
Date:   Thu Jun 4 17:47:19 2015 +0200

    Description 3

commit 3dddec8764330250cf7d57e2d18957e568a64c36
Author: Giovanni Antonioni <[email protected]>
Date:   Mon Jun 1 15:47:42 2015 +0200

I need to bring my project status to the first branch (3dddec87) and delete the other. How can i do from terminal?

Upvotes: 1

Views: 34

Answers (3)

axiac
axiac

Reputation: 72226

The output of git log that you provided doesn't say anything about branches. It displays the last four commits on the current branch.

If you really want to bring the project to the state it had on commit 3dddec87 then you can use git reset --hard

git reset --hard 3dddec87

This command discards the three commits you did after 3dddec87. It brings everything (working tree, index, current branch) in the state they were immediately after you created commit 3dddec87.

How to play safe

Before doing git reset --hard you can create a new branch (I named it backup) that points to your current HEAD:

git branch backup

It serves as a backup in case you do something wrong.

If you not are satisfied how the things look like after you do git reset --hard, you can return back where you were using git merge backup or git reset --hard backup.

But if you are satisfied and you decide you really don't need the first three commits from the listing you can delete the backup branch using:

git branch -D backup

Upvotes: 1

ivamax9
ivamax9

Reputation: 2629

It's can be done via

git checkout <commit>

So for you it will be

git checkout 3dddec8764330250cf7d57e2d18957e568a64c36

Of course if you want discarding local changes you can do, but be careful with it :

git reset --hard 3dddec8764330250cf7d57e2d18957e568a64c36

The second variant is better, because it's avoid ending up in detached head.

Thanks to RJFalconer for comment.

Upvotes: 1

nneonneo
nneonneo

Reputation: 179422

It sounds like you want to reset your branch back to that commit (discarding the later commits). In that case, you can do

git reset --hard 3dddec87

This sets both your branch and working copy to the specified commit.

You can use git reflog and another git reset command to undo this change.

Upvotes: 2

Related Questions