Advanced and Useful Git Notes

Babek Naghiyev
4 min readJun 25, 2023

--

Add alias:

git config --global alias.ac '!git add -A && git commit -m'

List remote repos

git remote

Get the URL for the remote repo (origin)

git remote get-url origin

1. Git logging

  • Log the last 3 commits
git log -3
  • Beautiful log
git log --graph --decorate --oneline
  • Search log with the text “Search string”
git log -S "Search string"
or
git rev-list --all | xargs git grep -F ‘’
  • Log with changed file names
git log --name-only
  • Log commits before the date and from author ‘nagibaba’
git log -n 1 -before=”01–04–2022" -author=”nagibaba”
  • See changes since 3 weeks ago
git log --no-merges --raw --since='3 weeks ago'
  • Search logs by commit messages:
git log — grep=’pattern’ --pretty=oneline
  • See the changes over time for a file
git log -p path/to/file/filename
  • See your git history as a graph
git log --pretty=format:”%cn committed %h on %cd”

Nagibaba committed 400e4b7 on Fri Jun 24 12:30:04 2014 -0500.

  • Log format main placeholders
%h
abbreviated commit hash

%an
author name

%ae
author email

%ah
author date, human style

%cn
committer name

%ce
committer email

%cd
committer date

%ch
committer date, human style

%b
body

More

  • Log all changes to a specific file.
git log --all -- path/to/file/filename
  • Log your actions
git reflog
  • Log commits included in branch1 that aren’t included in branch2
git log branch1 ^branch2

2. Stash

Add your changes to the stash to take later. Useful when you change the branch.

  • Stash last changes
git stash [-u, -a]
[-u for untracked and tracked files]
[-a for ignored, untracked, and tracked files]
  • List stash objects
git stash list
  • Take prior changes from the stash and remove them from the stash list(Popping).
git stash pop

or

git stash pop stash@{1} (to pop the second stash)
  • Add to stash with message
git stash push -m "Ordinary changes"
  • Take stash without removing it from the list
git stash apply stash@{1}

3. Checkout

  • To the previous branch
git checkout -
  • Switch to a branch.
git checkout <branchName>
  • Create a new branch from the current branch
git checkout -b <branchName>
  • Get a file from another branch
git checkout origin/master -- path/to/file/filename
  • Remove changes to a specific file
git checkout -- path/to/file/filename
  • Create a local branch from the remote repository
git fetch --all
git checkout <branchName>

4. Reset

  • Remove changes from staging and keep them in the index.
git reset

or

git reset HEAD
  • Remove from staging and index
git reset --hard

Remember

--soft keeps in staging, --hard removes from all areas.

  • Remove the last commit and keep the changes in the index
git reset HEAD~1
  • Remove commits after the last common commit with the master
git reset origin/master
  • Remove commits after a specific hash and save them in the index
git reset 0219bbee

5. Branch

  • Show local branches
git branch [-a, -r]
[-a to see remote branches too]
[-r to see only the remote branches]
  • Remove a branch
git branch -D <branch-name>
  • Search branches
git branch --all | grep <searchText>
  • Search for all branches that contain a specific commit
git branch -a --contains <commit-hash>
  • Find branches that are merged into master
git branch --merged master
  • View a file of another branch
git show master:thefile.js
  • Tip: I’d consider using it this way
# Fetch all branches to origin
git fetch --all
# See the file in the master branch of the origin
git show origin/master:thefile.js
  • Remove a remote branch
git push origin --delete <remote_branchname>
  • Rename a branch
git branch -m <new-branch-name>

6. Commit

  • Add and commit (Only for tracked files)
git commit -am "message"
  • Edit the last commit and add staged files to it
git commit --amend -m <commit message>
  • List files changed in a commit
git diff-tree --no-commit-id --name-only -r <commit-hash>
  • Undo a commit
git revert <commit-hash>
  • Reword the last commit
git commit -v --amend
  • Get the commit from another branch (Warning: The hash changes)
git cherry-pick <commit-hash>

# Get the last commit of a branch
git cherry-pick master
  • Add changes to the last commit (HEAD)
git add .
git commit --amend --no-edit

7. Diff

  • Diff of the index to the HEAD (The last commit)
git diff
  • See the diff in the stage
git diff --cached
  • Find text on the stage

git diff-index --cached -Ssearchtext HEAD
  • See the difference with another branch (master)
git diff master [--name-only]
  • See the difference between two branches
git diff branch1..branch2

8. Find and blame

  • Find who did changes in a file
git blame README.md
  • Using bisect you can detect the commit that started a bug
git bisect start                    # Start the search 
git bisect bad # Set the point to the bad commit
git bisect good v1.3.0. # Set the point to the good commit|tag
git bisect bad # Inform that the current state is bad
git bisect good # Inform that the current state is good
git bisect reset # Finish the search

Pls, clap and share, if you liked the article.

Take a look at other posts:

--

--

Babek Naghiyev
Babek Naghiyev

Written by Babek Naghiyev

Senior Software Engineer (Frontend)

No responses yet