My best Git Commands and Workflows

There’s a git command for that

Git commands aren’t always intuitive. Ilike to share my most used git commands. They would be super useful for accomplishing common tasks like creating or renaming a git branch, removing files, and undoing changes.

For each git command in the list, I´ll show you the commands that actually exist and that you can use to accomplish the same tasks. If you’re still learning Git, this list reads like a tutorial and is worth keeping as a cheat sheet.

How to change the git URL of the remote repository

git remote set-url origin new.git.url/here

How to create a git remote branch

git checkout -b <branch-name> # Create a new branch and check it out
git push <remote-name> <branch-name>

How to remove files from the new gitignore file

git rm -r --cached . && git add . && git commit -am "Remove ignored files"

How to remove all local branches that aren´t on the remote repository

git branch --merged >/tmp/merged-branches && vi /tmp/merged-branches && xargs git branch -d </tmp/merged-branches

Reset git local branch to remote branch

git fetch origin
git reset --hard origin/master

How to squash the last n commits together

git reset --soft HEAD~3 &&
git commit



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store