Pro Git commands you should know

Git Cherry-Pick 🍒

Imagine working on a branch and a coworker fix a bug, as usual, you have to wait to merge in develop branch the you should rebase to get that fix in your branch. But, when you rebase, you’ll have all the commits you don’t need.
All you need is the commit hash from the other branch. Then, you run git Cherry-Pick -x {commit hash}. This command will generate a new commit in you branch.
If you want the modification without commit. Then, you specify -n flag or –no-commit in the command.

git cherry-pick -x {commit hash}

Best way to git log

Git log is the command that show us all commits on an actual branch. But, this command will show more informations with colors 😎

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

To update commit message:

You misspelled a commit message, and you want to update it with a correct one. Add the -v flag to see more details about files changes.

git commit -v --amend

Clean repo from the untracked changes:

This command should be useful when you add specific files to your git repo. If you want to clean your repo from untracked changes, this command will do it.

git clean -f -d

Search in git:

Searching in git repo is possible. This command is list all the commits, then, search in each commit with the added query.

git rev-list --all | xargs git grep -F (query)

Remove sensitive file from git history:

You pushed a secret file? 😅You can run this command and replace the file name on it. You’ll see a warning ⚠️ But don’t worry. This should take a lot of time if the repo’s history if large.

git filter-branch --index-filter 'git rm --cached --ignore-unmatch <your file name>' --prune-empty --tag-name-filter cat -- --all

To optimize the repo:

The garbage collector is the best tool to optimise a repo. Add the –prune=now –aggressive to more disc space optimisation. The default value of 2 weeks ago, so we can override it with (now), you can configure this date in config gc.pruneExpire

git gc --prune=now --aggressive

Backup untracked files

You have a lot of files that you don’t want to push it right now, and you want to zip ’em all (pictures, inputs, large data), with this command you can archive them in a zip file using this command.

git ls-files --others --exclude-standard -z | xargs -0 tar rvf ~/

Count commits in a branch

This command is useful when you want to compare branches and you want to count commits in a branch.

git rev-list --count <branch-name>

Open conflicts in editor

git diff --name-only | uniq | xargs $EDITOR

Autocorrect typo

This config will correct your misspelled types for git commands like checkout, rebase, status.

git config --global help.autocorrect 1

Sort branches by date

When you have a large repo like open source projects, or big company project, you need to sort branches by date and this flag will be your gadget.

git branch --sort=-committerdate

Update from remote

Personally, I don’t use this command but, it’s useful when you want to reset your repo and synchronise it with the remote repo.

git fetch origin && git reset --hard origin/master && git clean -f -d

Reset the author in the commits:

Working remotely for different clients, it’s not that easy. I config repos locally to add username and client’s email. But, if you forgot to config locally the repo and start committing with your personal email, don’t worry. Just re-config your name and email and run this command. This will update your name and email without editing the last commit.

git commit --amend --reset-author --no-edit
You Might Also Like

Leave a Reply