Git for Noob-to-Pro — #3(Commits)

Search for a command to run...

No comments yet. Be the first to comment.
Bun, the hyper-fast JavaScript runtime known for its incredible speed and Zig-based architecture, is being completely rewritten in Rust. For a project that has over 22 million monthly downloads and ea

Next.js 16.3 is almost here, and it's packed with a ton of improvements and let's see them in this post 1.Instant Navigation For a while now, there’s been a valid, lingering debateServer Components vs

It’s official. React has merged its long-awaited Rust port of the React Compiler (formerly known as React Forget) into the main repository. What started as an experimental research project by Joseph S

If you follow modern technology trends, you have likely been led to believe that artificial intelligence is entirely built on Python. Every tutorial, machine learning framework documentation, and open

Just when the JavaScript ecosystem was catching its breath after last week’s TanStack Router compromise, the Mini Shai-Hulud supply chain worm has continued its relentless march. Developed by the thre

If you don’t have time to read but want to know what’s there in this series. Find the quick read 👇

We are going to see the commonly used Git snippets for the repository which will make you from Noob to Pro.
The Git Repository snippets that we are going to look at in this series are
Local Repository
Branches
Commits
Module
Stash
If you are new to this series check out Part 2(Branches)👇
In this post, we are going to see the topic Commits which has the following sub-topics like
View a short summary of commits
View commits by author
Move commits from the main to a new branch
View a short summary of commits without merging commits
Merge a branch and create a merge commit
View commits in a specific date range
Create a commit with a different date
Remove a file from last commit
Undo a commit
Undo the last commit

Prints a short summary of all commits.
git log --oneline to list a short summary of all commits.git log --oneline
# Example
git log --oneline
6f5680a (HEAD -> master, origin/master) Create README.md
a78dfef feat: add more layouts
e8b0c94 first commit
Prints all commits by the specified author.
Use git log --author=<author> to retrieve all commits by the specified <author>.
Use arrow keys to navigate, press Q to exit.
git log --author=
#Example
git log --author="nidhinkumar"
commit a78dfeff4539fd0eb5496da1dc6bc006c812c313
Author: nidhinkumar
Date: Sat Jul 9 20:08:29 2022 +0530
Moves local commits from the main branch to a new branch.
Use git branch <branch> to create a new branch at the tip of the current main.
Use git reset HEAD~<n> --hard to rewind back <n> commits and discard changes.
Use git checkout <branch> to switch to the new branch.
Only works if the changes have only been committed locally and not pushed to the remote.
git branch
git reset HEAD~ --hard
git checkout
# Example
git checkout main
git add .
git commit -m "Fix product bug"
git branch patch-1
# `patch-1` branch is created containing the commit "Fix product bug"
git reset HEAD~1 --hard # Remove the commit from `main`
git checkout patch-1
Prints a short summary of all commits excluding merge commits.
git log --oneline --no-merges to list a short summary of all commits without merge commits.git log --oneline --no-merges
# Example
git log --oneline --no-merges
4cc58df (HEAD -> master) v
241f9de test
6f5680a (origin/master) Create README.md
a78dfef feat: add more layouts
e8b0c94 first commit
Merges a branch into the current branch, creating a merge commit.
Use git checkout <target-branch> to switch to the branch into which you want to merge.
Use git merge --no-ff -m <message> <source-branch> to merge a branch into the current branch, creating a merge commit with the specified <message>.
git checkout
git merge --no-ff -m
# Example
git checkout main
git merge --no-ff -m "Merge patch-1" patch-1
# Merges the `patch-1` branch into `main` and creates a commit
# with the message "Merge patch-1"
Prints all commits in the specified date range.
Use git log --since=<date-from> --until=<date-to> to view a log of all commits between <date-from> and <date-to>.
You can use it only --since=<date-from> to see all commits since a specific date or only --until=<date-to> to view, all commits up to a specific date
Use arrow keys to navigate, and press Q to exit.
git log [--since=] [--until=]
# Example
git log --since='Jul 1 2022' --until='Jul 10 2022'
# commit 6f5680a56ca88776d1dfcc12842d2ef45f6e2529 (origin/main)
# Author: nidhinkumar06
# Date: Sat Jul 9 20:12:03 2022 +0530
# Create README.md

Sometimes, you might run into a situation where you need to create a commit with a different date than the current one. You can handle this using GIT_AUTHOR_DATE and GIT_COMMITTER_DATE:
GIT_AUTHOR_DATE='Wed Jul 20 19:32:10 2022 +0530' \
GIT_COMMITTER_DATE='Wed Jul 20 19:32:10 2022 +0530'\
git commit -m 'Commit from the past'
As shown in the example above, you can set both values to any date you like and your code will be committed on that date. Note that the format for the values above is 'date +"%s %z"', also referred to as internal raw git format, but you can also use other formats, such as RFC 2822 ('Wed, 20 Jul 2022 19:32:10 +0530'), ISO 8601 ('2022-07-20 19:32:10 +0530'), local ('Wed Jul 20 19:32:10 2022'), short ('2022-07-19') or relative (5.seconds.ago, 2.years.3.months.ago, '6am yesterday').
Removes a file from the last commit without changing its message.
Use git rm --cached <file> to remove the specified <file> from the index.
Use git commit --amend to update the contents of the last commit, without changing its message.
git rm --cached
git commit --amend
# Example
git rm --cached "index.html"
git commit --amend
# Removes `index.html` from the last commit
Undoes a specified commit without rewriting history.
git revert <commit> to revert the specified <commit>, creating a new commit with the inverse of the commit's changes.git revert
# Example
git revert 513cbff06fef
# Reverts the commit `513cbff06fef`
Undoes the last commit without rewriting history.
git revert HEAD to revert the last commit, creating a new commit with the inverse of the commit's changes.git revert HEAD
# Example
git revert HEAD
# Reverts the last commit
