Back to Home

Git Guide

Table of Contents

Overview

This document provides a compilation of useful Git commands.

Merging

Back to top

Renaming a Branch

Removing

Removing Files

Deleting a Local Branch

Sooner or later, you'll have a ton of old branches that have been merged on your local instance of a repo. To clean things up, use the following command:

git branch -d name-of-local-branch-to-delete

To force-delete a branch that has not been completely merged, you can use:

git branch -D name-of-local-branch-to-delete

Back to top

Stashing Changes

Back to top

Undoing a Commit

Only undo the act of committing and leave everything else intact (do not unstage files):

git reset --soft HEAD^

Undo the act of committing and unstage all files, but leave the work tree (your files) intact:

git reset HEAD^

Discard all changes and reset everything to the previous commit:

git reset --hard HEAD^

Back to top

Undoing one or more Pushes

First, reset the remote branch:

git push -f origin commit-hash-to-rewind-to:branch_name

Then, reset your local branch so it's at the same place as the remote:

git reset --hard commit-hash-to-rewind-to

IMPORTANT NOTE: Be very sure that this is absolutely essential for your purposes before doing it! You are rewriting history!

Back to top

Misc

Back to top