Skip to content

Instantly share code, notes, and snippets.

@mullnerz
Last active November 7, 2015 19:58
Show Gist options
  • Save mullnerz/5a45053c3a8281ea7982 to your computer and use it in GitHub Desktop.
Save mullnerz/5a45053c3a8281ea7982 to your computer and use it in GitHub Desktop.
git quick references
# Set default behaviour, in case users don't have core.autocrlf set.
* text
# Explicitly declare text files we want to always be normalized and converted
# to native line endings on checkout.
*.html text
*.css text
*.js text
# Declare files that will always have LF line endings on checkout.
*.sh text eol=lf
*.htaccess text eol=lf
# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
# Autodetect text files
* text=auto
# Force the following filetypes to have unix eols, so Windows does not break them
*.* text eol=lf
# You should do
git config branch.master.rebase true
# in the repo to keep your history nice and clean.
# You can set this globally using :
git config --global branch.autosetuprebase remote.
git config --global --list
user.name=Zoltán Müllner
[email protected]
core.autocrlf=true
branch.autosetuprebase=remote
push.default=simple
[user]
name = Zoltán Müllner
email = [email protected]
[core]
autocrlf = true
[branch]
autosetuprebase = remote
[push]
default = simple
[core]
eol = lf
autocrlf = input
GIT: remove local changes
# Hard reset
git reset --hard [HEAD]
# completely remove all staged and unstaged changes to tracked files.
# I find myself often using hard resetting, when I'm like "just undo everything like if I had done a complete re-clone from the remote". In your case, where you just want your repo pristine, this would work.
# Clean
git clean [-f]
# Remove files that are not tracked.
# For removing temporary files, but keep staged and unstaged changes to already tracked files. Most times, I would probably end up making an ignore-rule instead of repeatedly cleaning - e.g. for the bin/obj folders in a C# project, which you would usually want to exclude from your repo to save space, or something like that.
# The -f (force) option will also remove files, that are not tracked and are also being ignored by git though ignore-rule. In the case above, with an ignore-rule to never track the bin/obj folders, even though these folders are being ignored by git, using the force-option will remove them from your file system. I've sporadically seen a use for this, e.g. when scripting deployment, and you want to clean your code before deploying, zipping or whatever.
# Git clean will not touch files, that are already being tracked.
# Checkout "dot"
git checkout .
# I had actually never seen this notation before reading your post. I'm having a hard time finding documentation for this (maybe someone can help), but from playing around a bit, it looks like it means:
# "undo all changes in my working tree".
# I.e. undo unstaged changes in tracked files. It apparently doesn't touch staged changes and leaves untracked files alone.
# Stashing
# Some answers mention stashing. As the wording implies, you would probably use stashing when you are in the middle of something (not ready for a commit), and you have to temporarily switch branches or somehow work on another state of your code, later to return to your "messy desk". I don't see this applies to your question, but it's definitely handy.
# To sum up
# Generally, if you are confident you have committed and maybe pushed to a remote important changes, if you are just playing around or the like, using git reset --hard head followed by git clean -f will definitively cleanse your code to the state, it would be in, had it just been cloned and checked out from a branch. It's really important to emphasize, that the resetting will also remove staged, but uncommitted changes. It will whipe everything that has not been committed (except untracked files, in which case, use clean).
# All the other commands are there to facilitate more complex scenarios, where a granularity of "undoing stuff" is needed :)
Git: undo
# if you want to get rid of WORK IN PROGRESS. It will reset you back to the most recent commit, and erase all the changes in your working tree and index.
git reset --hard HEAD
# Careful: git reset --hard WILL DELETE YOUR WORKING DIRECTORY CHANGES. Be sure to stash any local changes you want to keep before running this command.
# Assuming you are sitting on that commit, then this command will wack it...
# The HEAD~1 means the commit before head.
git reset --hard HEAD~1
# Or, you could look at the output of git log, find the commit id of the commit you want to back up to, and then do this:
git reset --hard <sha1-commit-id>
# If you want to keep your work and just 'undo' that commit command (you caught before pushing to repo):
# Do not use the --hard flag unless you want to destroy your work in progress since the last commit.
# The HEAD~1 means the commit before head.
git reset --soft HEAD~1
# If you already pushed it, you will need to do a force push to get rid of it...
# However, if others may have pulled it, then you would be better off starting a new branch. Because when they pull, it will just merge it into their work, and you will get it pushed back up again.
# If you already pushed, it may be better to use git revert, to create a "mirror image" commit that will undo the changes. However, both commits will both be in the log.
git push origin HEAD --force
# If you just messed up your last commit (wrong message, forgot to add some changes) and want to fix it before pushing it to a public repo why not use:
# If you have newly staged changes they'll be combined with the last commit (that you're trying to get rid of) and will replace that commit.
# Of course if you amend a commit after you've pushed it, you're rewriting history so if you do that be sure to understand the implications.
git commit --amend -m "New message here"
# Windows users, if you have trouble compiling due to line endings then make sure you configure git to checkout the repository with lf (unix) line endings. This can be achieved by setting core.eol.
git config core.eol lf
git config core.autocrlf input
git rm --cached -r .
git reset --hard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment