Back

Explore Courses Blog Tutorials Interview Questions
+11 votes
2 views
in DevOps and Agile by (29.3k points)

I cloned a Git repository and then tried to roll it back to a particular commit early on in the development process. Everything that was added to the repository after that point is unimportant to me so I want to omit all subsequent changes from my local source code.

However, when I try to roll back in the GUI tool it doesn't update my local file system - I always end up with the latest source code for the project.

What's the correct way to just get the source for a repository as of a particular commit in the project's history and omit all later updates?

2 Answers

+7 votes
by (50.2k points)

To resolve this problem, use the following command

git reset --hard <tag/branch/commit_id>

Also, there are several things to take care of

git reset without the --hard option resets the commit history, but not the files. With the --hard option the files in the working tree are also reset. 

For more information, you can refer to this answer

https://intellipaat.com/community/2830/how-do-i-revert-a-git-repository-to-a-previous-commit?show=2830#q2830

+4 votes
by (62.9k points)

Because of changes to how tracking branches are created and pushed I donot suggest renaming branches.

This is what I recommend:

Make a copy of the branch at its current state:

git branch crazyexperiment

(The git branch command will leave you with your current branch still checked out.)

Reset your current branch to your desired commit with git reset:

git reset --hard c2e7af2b51

(Replace c2e7af2b51 with the commit that you want to go back to.)

When you decide that your crazy experiment branch does not contain something helpful, you can delete it with:

git branch -D crazyexperiment

It's always nice when you are beginning out with history-modifying git commands (reset, rebase) to create backup branches before you run them.

Eventually, once you're comfortable you won't find it necessary. If you do modify your history in a way that you just don't desire and haven't created a backup branch, look into git reflog. Git keeps commits around for quite a while even though there aren't any branches or tags pointing to them

Browse Categories

...