Git Commands (that not every developer knows) - part 2
In my previous post we saw some very useful commands for our day-to-day with git but there is more! In this post, I am going to tell you about 3 extra git commands that you will love.
1. git shortlog
with options #
This command shows you a summarized log that is by default grouped by author.
# Shows the full list of commits grouped by author
git shortlog
# Shows the full list of commits with the hash of a specific author
git shortlog --format="%h | %s" --author="The author"
# Shows the number of commits per author sorted from highest to lowest number
git shortlog -sn
If you want to check the commits from somebody you can use shortlog.
2. git add -p
to interactively choose hunks #
A lot of developers use “git add .” and in some cases specify the files and create different commits but what about adding an specific change in a file, that’s where “-p” comes to the rescue. It helps you to interactively choose hunks to add to the index.
# Let's start the process:
git add -p
# You will see a prompt with options like this for the hunk
diff --git a/src/core/annotation.js b/src/core/annotation.js
index 9f2719499..6809128ed 100644
--- a/src/core/annotation.js
+++ b/src/core/annotation.js
@@ -37,6 +37,10 @@ import {
Util,
warn,
} from "../shared/util.js";
+import { BaseStream } from "./base_stream.js";
+import { bidi } from "./bidi.js";
+import { Catalog } from "./catalog.js";
+import { ColorSpace } from "./colorspace.js";
import {
collectActions,
escapeString,
(1/5) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?
The options are:
- y - stage this hunk
- n - do not stage this hunk
- q - quit; do not stage this hunk nor any of the remaining ones
- a - stage this hunk and all later hunks in the file
- d - do not stage this hunk nor any of the later hunks in the file
- g - select a hunk to go to
- / - search for a hunk matching the given regex
- j - leave this hunk undecided, see next undecided hunk
- J - leave this hunk undecided, see next hunk
- k - leave this hunk undecided, see previous undecided hunk
- K - leave this hunk undecided, see previous hunk
- s - split the current hunk into smaller hunks
- e - manually edit the current hunk
- ? - print help
This is the proper way to add changes to the index, it’s like using the feature in source tree or git kraken tools, choosing which parts to include in the commit.
3. git notes
to add notes to commits #
Why notes? here are a two reasons:
- Add more information about why a commit has specific changes
- Leave a mark in the commit you can search later to go that specific commit
# Add a note to the current commit
git notes add -m "Message"
# Add a note to an specific commit
git notes add -m "Message" 72a144e2
# Get the list of notes:
git notes list
# Get a better presentation of the notes with commit info (add it to an alias)
git notes list | awk '{if(NF>1) print $2}' | while read commit; do git log -1 --pretty=format:'%h %s%n%b%nNotes: %N' $commit; done | grep -v 'Notes: $'
Keep in mind that a commit message says a lot about the developer.
4. git interpret-trailers
to add extra data at the end of the commit #
Let’s say you want to add a “Signed-off-by: Alice alice@example.com” to a commit as part of the review process, you can do it with git interpret-trailers.
# Add a message to the end of a commit:
git interpret-trailers --append "Signed-off-by: Alice <alice@example.com>" --in-place 72a144e2
This helps you to add more info to a commit easly.
5. git log --all --graph --decorate --oneline --simplify-by-decoration
to get a good log presentation #
Not much to explain here, but it is pretty useful sometimes.
# Get a better log representation:
git log --all --graph --decorate --oneline --simplify-by-decoration