Skip to main content

Git Cheatsheet

Β· 5 min read
Carlos Angulo Mascarell

This post explains the next topics:

  • the git commands I use the most
  • some git alias to avoid typing long commandsπŸ˜πŸ‘Œ

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

[alias]
b = branch

Next are my personal alias:

Alias configured

[alias]
b = branch
bl = branch --list
bd = branch -D
co = checkout
c = commit -am
cw = commit -am wip
cob = checkout -b
cor = "!f() { git checkout --track origin/${1-No Argument Provided}; }; f"
cp = cherry-pick
cp-c = cherry-pick --continue
cp-a = cherry-pick --abort
re = "!f() { git rebase -i HEAD~${1-No Argument Provided}; }; f"
re-c = rebase --continue
re-a = rebase --abort
re-m = rebase -i --rebase-merges
pushf = push --force
pushr = "!f() { currentBranch=$(git branch --show-current);git push --set-upstream origin $currentBranch; }; f"
pushrf = "!f() { currentBranch=$(git branch --show-current);git push --set-upstream origin $currentBranch --force; }; f"
fix = "!f() { git add .; git commit --amend --no-edit; git pushrf;}; f"
dr = "!f() { git reset --hard HEAD~1;}; f"
undolastco = reset --soft HEAD~1
st = status
l = "!f() { git log --oneline -n ${1-15}; }; f"
lg = "!f() { git log --oneline --grep=${1-No Argument Provided}; }; f"
settings = config --global --edit

Check the file here

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.

Please note the l and lg are alias defined as Bash functions with input parameters ${1-DefaultParameter}.

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 Software Engineer with experience as Developer and DevOps. The technologies I have worked with are DotNet, Terraform and AWS. For the last one, I have the Developer Associate certification. I define myself as a challenge-seeker person and team player. I simply give it all to deliver high-quality solutions. On the other hand, I like to analyze and improve processes, promote productivity and document implementations (yes, I'm a developer that likes to document πŸ§‘β€πŸ’»).

You can check my experience here.

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

Did you like it? Share It!


Comments