Git Guide
Table of Contents
- Overview
- Merging
- Removing
- Renaming a Branch
- Stashing Changes
- Undoing a Commit
- Undoing one or more Pushes
- Misc
Overview
This document provides a compilation of useful Git commands.
Merging
- Merge all files from source branch into target branch
git checkout target-branch git merge source-branch
Alternate method (same results)
git merge source-branch target-branch
Merge single file from source branch into target branch
git checkout source-branch/path/to/file git commit git push
Renaming a Branch
- Rename your local branch
git branch -m new-name
- Delete old-name remote branch and push the new-name local branch
git push origin :old-name new-name
Reset the upstream branch for the new-name local branch
git push origin -u new-name
Alternate method (same results)
git branch -m old_branch new_branch # Rename branch locally git push origin :old_branch # Delete the old branch git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
Removing
Removing Files
- Remove all untracked files
git clean -f
- Remove all untracked files and directories
git clean -fd
- Get rid of all changes on your local branch
git reset --hard HEAD
- Reset single file in local branch to remote branch
git checkout remote-branch -- file1/to/reset file2/to/reset git commit git push
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
Stashing Changes
- Stash all changes to the repo
git stash
- Delete the top stash
git stash drop
- Delete a specific stash
git stash drop stash@{n}
- Grab all code from last stash but don't remove it from the stash list
git stash apply
- Grab all code from last stash and remove it from the stash list
Note:git stash pop
git stash pop
=git stash apply
+git stash drop
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^
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!
Misc
- Fix errors that contain the phrase
cannot lock ref
git remote prune origin
- Show a history of what was committed and when
git log
HEAD
refers to the current commit, which is usually the tip of the currently checked-out branch.HEAD^
is the commit before the current one. (In Git, the ^ means "the one before").