Skip to content

Instantly share code, notes, and snippets.

@pudgereyem
Last active June 15, 2018 09:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pudgereyem/73fa4066cb1915503eb3 to your computer and use it in GitHub Desktop.
Save pudgereyem/73fa4066cb1915503eb3 to your computer and use it in GitHub Desktop.
GIT: Useful commands
# If you regret all the changes you've done (in the HEAD)
# Sort of like a huge/massive CMD-Z (undo).
git reset --hard HEAD
### ---
# If you have commited changes, and then end up regretting that,
# because you want to add new changes to that very commit.
# First add files
git add .
# And then just commit with the --amend argument.
git commit --amend
### ---
# Making git “forget” about a file that *was* tracked but is now “.gitignored”
# <http://stackoverflow.com/questions/1274057/making-git-forget-about-a-file-that-was-tracked-but-is-now-gitignored>
# .gitignore will prevent untracked files from being added (without an add -f) to the set of files tracked by git, however git will continue to track any files that are already being tracked.
# To stop tracking a file you need to remove it from the index. This can be achieved with this command.
git rm --cached <file>
git add . # or git add <file>
# The removal of the file from the head revision will happen on the next commit.
### ---
# Remove untracked files from the working tree
# Link: http://git-scm.com/docs/git-clean
# Stack overflow Question: http://stackoverflow.com/questions/61212/remove-local-untracked-files-from-my-current-git-branch
# Helpful if files has been added accidentaly. Use --dry-run to check which files are going to get deleted
git clean -f --dry-run
### ---
# Remove a git submodule
# Link: https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial#Removal
# Stack Overflow Question: http://stackoverflow.com/questions/1260748/how-do-i-remove-a-git-submodule
# Best Answer: http://stackoverflow.com/a/1260982
# To remove a submodule you need to:
# - Delete the relevant line from the .gitmodules file.
# - Delete the relevant section from .git/config.
# - Run git rm --cached path_to_submodule (no trailing slash).
# - Commit the superproject.
# - Delete the now untracked submodule files.
### ---
# Find out which branches has change a certain file
# Source: https://stackoverflow.com/a/6258472/794103
FILENAME="<filename>"
git log --all --format=%H $FILENAME | while read f; do git branch --contains $f; done | sort -u
### ---
# To delete local branches which have alread been merged into master:
# https://stevenharman.net/git-clean-delete-already-merged-branches
git branch --merged master | grep -v "\* master" | xargs -n 1 git branch -d
@pudgereyem
Copy link
Author

To amend a commit

git add .
And then just commit with the --amend argument.
git commit --amend

source: http://nathanhoad.net/git-amend-your-last-commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment