11 Git tips to level up your workflow

Tip #1

Keep your commits organized

git add -p <filename>

Tip #2

View branch sorted by date

# sort desc (-committerdate)
git branch --sort=-committerdate
# sort asc (committerdate)
git branch --sort=-committerdate
git for-each-ref --sort='-committerdate' --format='%(refname)%09%(committerdate)' refs/heads
refs/heads/branch_name commitdate
refs/heads/branch_name2 commitdate
# real output
refs/heads/new_design Wed Apr 20 12:24:21 2022 +0200
refs/heads/design_assets Wed Apr 20 11:30:30 2022 +0200
git for-each-ref --sort='-committerdate' --format='%(refname)%09%(committerdate)' refs/heads | sed 's-refs/heads/--'#output
branch_name commitdate
branch_name2 commitdate
#real output
new_design Wed Apr 20 12:24:21 2022 +0200
design_assets Wed Apr 20 11:30:30 2022 +0200

Tip #3

View merged or un-merged branches

git branch --merged master
git branch --merged
git branch --no-merged
git branch -a --no-merged

Tip #4

Creating aliases

git config --global alias.<alias> <command># Exgit config --global alias.s 'git status'

Useful aliases

Tip #5

Create a commit with a different date

GIT_AUTHOR_DATE='Mon May 18 19:32:10 2020 -0400' \
GIT_COMMITTER_DATE='Mon May 18 19:32:10 2020 -0400'\
git commit -m 'Commit from the past'

Tip #6

Create a commit by a different author

  • Use git commit -m <message> to create a new commit with the specified <message>.
  • Use the --author option to change the <name> and <email> of the commit's author.
git commit -m <message> --author="<name> <email>"git commit -m "Fix the network bug" --author="Duck Quackers <cool.duck@qua.ck>"

Tip #7

View differences in changes

  • Use git diff to view the differences between your unstaged changes and the last commit.
  • You can use the --staged option to view differences between your staged changes and the last commit instead.
git diff [--staged]# Displays the differences between unstaged changes and the last commit
git diff
# Displays the differences between staged changes and the last commit
git diff --staged

Tip #8

Take me back (when everything worked!)

git reflog
git reset HEAD@{index}
# Doesn’t modify the index or the working tree, leaving your changes staged for commit.git reset --soft# Use with caution, as it resets both the index and working tree. Uncommitted changes and all commits after will be removed.git reset --hard

Tip #9

View a visual graph of the repository

  • Use git log --pretty=oneline --graph --decorate --all to view a visual graph of the whole repository's history.
git log --pretty=oneline --graph --decorate --allgit log --pretty=oneline --graph --decorate --all
# * 3050fc0de Fix typo
# * c191f90c7 Initial commit

Tip #10

See commits in a specific 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.
git log [--since=<date-from>] [--until=<date-to>]git log --since='Apr 1 2021' --until='Apr 4 2021'
# commit c191f90c7766ee6d5f24e90b552a6d446f0d02e4
# Author: 30 seconds of code
# Date: Tue Apr 6 11:11:08 2021 +0300
# [...]

git log --since='2 weeks ago'
# commit c191f90c7766ee6d5f24e90b552a6d446f0d02e4
# Author: 30 seconds of code
# Date: Tue Apr 6 11:11:08 2021 +0300
# [...]

Tip #11

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>.
  • The --no-ff flag prevents git merge from executing a "fast-forward" if it detects that your current HEAD is an ancestor of the commit you're trying to merge. A fast-forward is when, instead of constructing a merge commit, git just moves your branch pointer to point at the incoming commit. This commonly occurs when doing a git pull without any local changes.

So when I can use --no-ff ?

Might be you’re asking now what’s the difference between fast-forward and rebase merge?

--

--

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
Abdlrahmansaber

Abdlrahmansaber

Software engineer, Problem solver, Geek.