Skip to main content

Command Palette

Search for a command to run...

Chapter 2: Git Commit Management: Log, Show, Amend, and Reset Explained

Understand how Git tracks files and commits using logs and history commands and Learn to fix mistakes, amend commits, and manage changes.

Updated
3 min read
Chapter 2: Git Commit Management: Log, Show, Amend, and Reset Explained
B

Software Engineer

Git Ignore:

what is the purpose of .gitignore file?

If we want to exclude specific files from being tracked and untracked, we can list those file names in the .gitignore file.

Git log:

git log: To get complete info about the commits we made.

git log -2: To get complete info about the last 2 commits we made.

git log —oneline: git log will return complete info about the commits we made. but Instead, we want to get only the half commitId and commit message, we can use this command.

git log —pretty=oneline: To get complete commitId and commit message, we can use this command.

How many commits have been made for a specific file? How can we find out? What is the command for this?

git log —follow —all filename: To get all the commits we made for this specific file. or we can also use git log filename but it will not be accurate if it has less commits.

Git show:

How many files have been committed for any commitId? How can we find out? What is the command for this?

git show commitId —name-only: To get only the filenames along with commit message.

git show commitId —stat: To get overall number of changes in the file, that is the total number of modification stats in that commit ID.

Git amend:

what does it mean amend in git?

--amend means: modify the most recent commit

git commit —amend —author “authorname <authoremail>”: To change the author for latest commit.

git commit —amend -m “authorname <authoremail>”: To change the commit message for latest commit.

If I committed 4 out of 5 tracked files and forgot to add the 5th tracked file in the same commit, but I need to include the 5th file in the same commit ID, and want all commits to be under the same commit ID, what is the command for this? In short, how to attach all tracked files in staging area to the latest commitId in this case?

git commit —amend —no-edit

How to change the last recent commit id message ?

git commit —amend

Git reset:

--hard means: move everything back and discard changes completely

git reset —hard HEAD~1: to delete the last commit.

in this command, HEAD represents latest commit, and HEAD~1 means last commit, if we give HEAD~2 it means, last 2 commits

On a final note, we are telling Git to discard the complete changes of the last specified number of commits. it will notify HEAD is now at below commit after delete of recent commit.

Git soft:

--soft means: only the commit will be deleted, but not the data, so the data is safe.

git reset —soft HEAD~1: to delete the last commit, but the file will not be deleted.

Git & GitHub Series: A Complete Guide from Beginner to Advanced

Part 2 of 3

In this series, I will guide you through the essential Git commands every DevOps engineer should know. I will also explain Source Control Management (SCM) using real-world, day-to-day use-case scenarios, presented clearly in a chapter-wise format.

Up next

Chapter 3: Understanding Git Branches and Branching Workflows

Understand Git branches from scratch with clear examples. Create, switch, and merge branches without breaking your code.