Be your own Git superhero! The Git commands that will save the day.

Posted 23 months ago by Michael Caruana

2 Minute(s) to read

Posted in

Approximate reading time: 3 minutes

Git has been a revolution for programmers. Shared repositories with a complete history of changes, easy deployment and nuanced management? Thank you very much! It all works heroically well when you're doing standard things, but run into a Git villain and you fall hard. Despite the various IDE integrations, shell integrations and standalone UI software on offer, the most reliable way to rescue your code is the command line interface. Here's a few commands that might save your day.

What is the status of the local repository - which branch, any changes to commit?

git status

So uh, where is the remote repository?

git remote -v

Commit all changes (includes modified, moved & removed files).

git commit -a -m "[comment]"

Whoops! Undo the last commit, make it look like nothing happened.

git reset --hard HEAD~1

Discard any uncommitted changes. We don't need these villains.

git checkout .

Fight! Fight! Resolve a merge conflict by banishing the bad guy...

git checkout --[ours|theirs] [filename]

...or manually resolve the conflict, blow by blow, by editing the file to remove the wayward code marked with <<<<<<< [our code] ======= [their code] >>>>>>> blocks, then...

git add [filename] include the new good guy, so the change can be committed.

Sometimes the hero resorts to time travel to prevent a crime. Undo the last two commits but leave the changes so we can keep working on it.

git reset --soft HEAD~2

Shine your hero signal into the sky! Identify critical code changes with a Tag. Here's how to create & push a Tag.

git tag [tag number]
git push origin [tag number]

That should be enough to justify wearing your pants on the outside! If you have any super git commands that should be added to our list, let us know in the comments below.