Skip to main content

Git Cheatsheet

· 4 min read
Carlos Angulo Mascarell
Senior Platform Engineer

This post explains the git commands I use the most and how I configure my git alias.

Git Commands

Git Checkout

git checkout --track origin/NAME

  • Create a local branch that tracks the remote one

git checkout -

  • Checkout the previous branch, this can be useful if you go from your feature branch to dev, you update it, and you want to come back to your feature branch to rebase dev

git checkout tags/<tag> -b <branch>

  • Checkout a branch starting from the tag provided

git switch

  • New git command to change between branches to avoid using checkout. Currently is in status experimental.
  • Check this link and this one

Git Branch

git branch --list [<pattern>]

  • List all the branches that match the <pattern>. e.g. *-1234 list all the branches that end 1234

Gitk

gitk

  • Check Last commits using a GUI

gitk <filePath>

  • Check <filePath> last commits using a GUI

Git Cherry Pick

git cherry-pick -n <commit>

  • Let's you inspect the files modified in the commit before cherry-pick them. Reference

Git diff

git diff [target-branch] -name-status

  • List the files modified and what kind of modification (Added, Deleted, Modified) in your branch using the target branch as reference

git diff [target-branch] -name-only

  • List the files modified in your branch using the target branch as reference

Git rebase

quick rebase commands:

git fetch
git rebase -i origin/main # remove the -i if you don't want to edit the commits
git push --set-upstream origin "YOUR_BRANCH_NAME" --force
  • cancel rebase: git rebase --abort

rebase since first commit

git rebase -i --root

How to configure an Alias

Using Command Line

CommandExample
git config --global alias.[AliasName] [AliasValue]git config --global alias.co checkout

Editing the git config file

  1. Open your .gitconfig file, it is located in your home folder
  2. In a new line, add the tag [alias]
  3. In the next lines add your alias following the format [TAB space]AliasName = AliasValue.

Please note this config file is space/tab sensitive so be sure no spaces are added at the end of each line

A short example:

[alias]
co = checkout
cob = checkout -b
st = status
re = "!f() { git rebase -i HEAD~${1-No Argument Provided}; }; f"

The one I use the most is cob, every time I have to create a new branch I type git cob [BRANCH_NAME]. Also the rebase one, git re develop, that is how I update my branch with the last changes from develop.

👉 See my full git alias & .gitconfig in Git Config & Alias.

How to update the permissions for bash files in a git repository

git update-index --chmod=+x path/to/file

$bashScripts= Get-ChildItem -Name -Filter "*.sh" -Depth 5
foreach ($script in $bashScripts) {
git update-index --chmod=+x $script
}

undo change in file permissions

Scenario: you copy and paste repositories manually. When you copy it from a external drive the permissions change for all the files

How to list differences? git diff --summary

Solution:

function git_undo_permissions_change_in_repo() {
git diff -p -R --no-ext-diff --no-color | grep -E "^(diff|(old|new) mode)" --color=never | git apply
}

function git_undo_permission_change_in_subfolders() {
# 1 level deep
for d in ./*/ ; do (cd "$d" && git diff -p -R --no-ext-diff --no-color | grep -E "^(diff|(old|new) mode)" --color=never | git apply); done
# 2 level deep
for d in ./*/*/ ; do (cd "$d" && git diff -p -R --no-ext-diff --no-color | grep -E "^(diff|(old|new) mode)" --color=never | git apply); done
}

References

About me

I'm a Senior Platform Engineer. I build and evolve Internal Developer Platforms — golden paths, self-service tooling, and paved roads spanning AWS governance, CI/CD standardization, and developer enablement — so engineering teams ship faster, safer, and more autonomously, with less cognitive load. My core stack is AWS, Terraform, and GitHub Actions, and I'm an AWS Certified Solutions Architect. I care deeply about paved roads, productivity, and documenting implementations so knowledge scales with the team (yes, I'm an engineer who likes to document 🧑‍💻).

You can check my experience here.

Personal Blog - cangulo.github.io GitHub - Carlos Angulo Mascarell - cangulo
LinkedIn - Carlos Angulo Mascarell

Did you like it? Share It!


Comments