Reviewing Investigating Changes to the REPO using CLI - jsanderson104/GIT-CheatSheet GitHub Wiki
To see a quick history of the changes to the repo:
git log
Each of the shown "commits" will generate a HASH. We use this hash because it's like a memory address that can take you back to that point or be referenced later. Which is what we're doing next.
To get details about the changes via a diff-like output:
git show [HASH]
If you want to see the changes to the files since the last commit:
git diff
- The above command will show all files modifications since the last commit.
You can, alternatively, provide a filename if you just want to see the changes for that specific file
git diff README.md
Oops! I accidentally committed a file/change that I didn't want in the next push... What do I do. Well, it's good you caught it before the push, we can fix it w/o much headache now, locally. The next command will take you back exactly 1 commit "in time".
git reset HEAD~1
- NOTE: There is a soft (default) mode that will keep the changes of your files. Using the
--hardoption will delete the changes you made since your last commit. This includes actually deleting or add back files from the filesystem (not just the repo). Use it with caution. - You can go back more than ~1... you can change that to a 3 or however far you want to go back. IMPORTANT: This will delete the commit history info so if you're sharing a repo with a large codebase that could mess them up.
A better, more modern way of removing the changes is with:
git revert [HEAD]
- This leaves your history intact while backing out the changes you made.
Looking for a specific commit.... you can search the commit messages for a REGEXP pattern like this:
git log --grep='patternhere'
If you want to reset to a particular commit. You can also use a combo of 2 commands:
- "git reflog"
- "git reset"
- In this case, however, we don't use the Checksum value like before. This time, we use the HEAD reference points.
Here's output of a sample "git reflog" command:
[automation@control newrepo]$ git reflog
08dd041 (HEAD -> main, j) HEAD@{0}: reset: moving to 08dd0411568d5e488ee2621d5b113e8f3460ab8b
08dd041 (HEAD -> main, j) HEAD@{1}: Branch: renamed refs/heads/master to refs/heads/main
08dd041 (HEAD -> main, j) HEAD@{3}: commit (initial): new repo first commit
[automation@control newrepo]$
Let's say that I wanted to revert back to the "new repo first commit" - basically reset the repo to original. I could do that like this:
git reset --hard HEAD@{3}
Remember: The --hard option DELETES and PUTS files back the way they were at that point. So if you have changes, files that you want to keep - you better copy them outside of the repo for now.