YavarTechWorks
Published in

YavarTechWorks

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.

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.

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.

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.

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

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.

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:

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.

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.

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.

Congratulations!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nidhin kumar

As a species, we have an inbuilt need to connect with others to communicate and share.