0

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Can you explain in more detail what you mean by "revert to an old commit and pull those files into the master"? Do you have another branch other than `master`? Where is the commit in that branch's history? – Code-Apprentice Feb 12 '18 at 16:49
  • @Code-Apprentice Sorry, still learning git. Basically I initialized a bare repository on the 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. – user8876747 Feb 12 '18 at 16:55
  • Please edit your question to include these details. – Code-Apprentice Feb 12 '18 at 16:56
  • 1
    I suggest that you learn about branches. Chapter 3 of [Pro Git](https://git-scm.com/book/en/v2) is a great place to start. You might also want to read Chapter 2 to fill in any gaps you may have with fundamentals. – Code-Apprentice Feb 12 '18 at 16:58

1 Answers1

0

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268