Now that we know about commit hashes, we can use them to switch to a specific commit. Switching between commits allows us to easily move between different historical versions of the project. For example, if you introduce a change that causes issues, and you want to use an earlier version, switching between commits will let you do so.
Commit hashes are preserved between the local repo and the remote repo. For instance, let's say we have two commits, c12
and c53
:
RemoteLocalc12c12README.mdgitcloneREADME.mdThisisaThisisaREADME.README.gitcommitc53README.mdc53gitpushThisisthebestREADME.mdREADME!ThisisthebestREADME!
c12
originally existed on the remote, but when we pulled it locally, the commit kept the same hash. This is because the commit is the same on both the remote and locally -- the same changes were made to the same files. When we changed a file and made a commit locally, it was given the hash c53
. When we later pushed this commit to the remote, it kept the same hash, because it was still the same commit. In the above diagram, both the local repo and the remote repo have two commits, c12
and c53
. We can switch between commits in the local repo without changing what commits are in the remote repo. We can do this with the git reset command:
LocalWorkingDirectoryc12README.mdREADME.mdThisisaThisisaREADME.README.gitcommitc53README.mdREADME.mdThisisthebestThisisthebestREADME!README!gitreset--hardc12c12README.mdREADME.mdThisisaThisisaREADME.README.
As you can see above, the commit is on the left, and what your working directory looks like on the right. If you use git reset --hard c12
, it switches back to the commit c12
, and changes all the files in the working directory so that they are the exact same as the files in the commit. This will essentially let you rewind the repo to past commits if there are problems with commits, or if you want to see what the project looked like at an earlier point in time.
The --hard
flag resets the working directory and the git history to a specific state. If we omitted the flag, or instead used the --soft
flag, it would keep the working directory the same, and only reset the git history.
Instructions
- Use the
git log
command to find the commit hash corresponding to the oldest commit in thechatbot
repo. - Use
git reset
to reset thechatbot
repo to the oldest commit. - Explore
README.md
, and see what text is contained in it
/home/dq/chatbot$ git reset
/home/dq/chatbot$ cd /home/dq/chatbot
/home/dq/chatbot$ HASH=`git rev-list --max-parents=0 HEAD`
/home/dq/chatbot$ git reset --hard $HASH
HEAD is now at e8dd5e9 Add the initial version of README.md