Collection of Useful Git Commands

Git commands & aliases

Update multiple repositories from a shared parent directory

One way to do it is to cd into each directory and run git pull but that can be tedious. Instead, you can simplify your workflow by running the following command from the parent directory:

ls | xargs -I{} git -C {} pull

Notes:

  • you can modify the command to run any git command you want(careful though)
  • run it in parallel ls | xargs -P10 -I{} git -C {} pull
  • add it to your .gitconfig or .zshrc file gdall = "!f() { ls | xargs -I{} git -C {} pull; }; f"} now you can run git gdall <COMMAND>... use pull, checkout or whatever you want.

Checkout to a new branch

git checkout -b <BRANCH_NAME>

Update origin main without adding a new commit

git stash && git pull origin main && git stash pop

Delete a branch

git branch -D <BRANCH_NAME>

Add and commit in one command

git commit -am "<COMMIT_MESSAGE>"

Reset to a previous commit

git reset --hard <COMMIT_HASH>

Cherry pick a commit

git cherry-pick <COMMIT_HASH>

aliases

Disclaimer:

If you want all these aliases and much more, I recommend you to install oh-my-zsh and use the git plugin.

g=git
ga='git add'
gaa='git add --all'
gam='git am'
gb='git branch'
gbD='git branch -D'
gba='git branch -a'
gbd='git branch -d'
gbl='git blame -b -w'
gcam='git commit -a -m'
gcb='git checkout -b'
gcmsg='git commit -m'
gl='git pull'
gpsup='git push --set-upstream origin $(git_current_branch)'