Git: Pull old commit into master

2.3k Views Asked by At

I initialized a bare repository on my shared drive and cloned it onto my local drive. I make changes locally, add it, commit it, and push it (git push origin master). I do the same thing on another computer and just git pull to get any changes between computers. I am trying to learn how I would revert back to an old "version" of my code in case I needed to.

First I checkout

git checkout "commit #"

then I try to git add -A and git commit -m "msg"

When I git push origin master I get "everything is up to date" and when I git checkout master and try to pull the old changes on the master it doesn't get them. Any idea on how I can get this to work?

1

There are 1 best solutions below

0
Code-Apprentice On

Commits are referenced by SHA-1 hashes. You can use these hashes to refer to any commit. For example, you can check out a specific commit with

git checkout <sha-1 hash>

where <sha-1 hash> is a valid hash for any commit in your repo. Be careful with this command as any new commits you make do not belong to any branch. If you want to make changes and commit them, you should create a branch first:

git checkout -b new-branch

Change new-branch to any name that describes the work you are doing.

If you want to reset master to a previous commit, then you can use

git checkout master
git reset <sha-1 hash>

This will keep all changes in your local copy while dropping all of the commits you made. If you want to purge all of the changes as well, use --hard:

git reset --hard <sha-1 hash>

Now you will need to force push to your remote:

git push -f origin master

Be very careful with these commands. If you are working with a team and any of your team members have pulled your previous changes, they will still have a local copy of them. Force pushing to master will cause conflicts with everyone else's local copies and just overall cause headaches for everyone.