Skip to main content

Command Palette

Search for a command to run...

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

Updated
5 min read
Git for Noob-to-Pro — #3(Commits)

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

On this Series

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

  1. Local Repository

  2. Branches

  3. Commits

  4. Module

  5. Stash

If you are new to this series check out Part 2(Branches)👇

On this Post

In this post, we are going to see the topic Commits which has the following sub-topics like

  1. View a short summary of commits

  2. View commits by author

  3. Move commits from the main to a new branch

  4. View a short summary of commits without merging commits

  5. Merge a branch and create a merge commit

  6. View commits in a specific date range

  7. Create a commit with a different date

  8. Remove a file from last commit

  9. Undo a commit

  10. Undo the last commit

1. View a short summary of commits

Prints a short summary of all commits.

  • Use 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

2. View commits by author

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

3. Move commits from main to a new branch

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

4. View a short summary of commits without merging commits

Prints a short summary of all commits excluding merge commits.

  • Use 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

5. Merge a branch and create a merge 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"

6. View commits in a specific date range

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

7. Create a commit with a different date

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').

8. Remove a file from the last commit

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

9. Undo a commit

Undoes a specified commit without rewriting history.

  • Use 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`

10. Undo the last commit

Undoes the last commit without rewriting history.

  • Use 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

Congratulations!

More from this blog

Nidhin's blog

163 posts

✨Crafting Code with a Smile for 8 Years:) Merging the Formal Dance of Angular, the Playful Rhythms of React, and the Next-level Moves of Next.js 🚀