In this day and age, nearly everyone uses git. From designers to engineers we all depend on it. Despite git being a really good version control system, enevitably we do sometimes mess up and have to fix our configuration and/or working tree, or just plain forget how to do that one thing we rarely do.
Change Authentication Timeout
Git can remember your https credentials if you aren’t using a ssh key, integrates nicely with the native keychain. The default timeout is rather short however, this can be changed via
git config --global credential.helper "cache --timeout=<SECONDS>"
For a full work day, this could be set to 28800.
Change Message of the Previous Commit
To fix a typo, or other error in the previous commit before pushing to remote,
we can simply add
--amend to the commit command and we will get the editor again.
git commit --amend
Add a File to the Previous Commit
To add a file that we forgot to add in the previous commit, we can simply add it
git add <filename>, then call
git commit -amend to add the changeset to the latest commit.
git add <filename> git commit --amend
Undo Changes to a File before a Commit
git reset <filename>
Undo the Previous Commit
To undo the most recent commit we can run
git reset HEAD~1.
If we pass
--soft option the working tree will be kept intact letting us
git add and friends to do the commit again.
git reset --soft HEAD~1
To remove the commit completely, resetting the changed files back to their
original state we can pass the
git reset --hard HEAD~1
Clean the Working Tree
To remove files that are not being tracked, we can use
git clean --force
Change Author of the Previous Commit
To fix the author after, for example forgetting to set
user.email. We can run
git commit with the
after having updated the configuration fields.
git config user.name "Your name" git config user.email firstname.lastname@example.org git commit --amend --reset-author
Change Anything Else
To fix anything else like squashing, removing, rewording and re-ordering commits,
git rebase with the
--interactive option provides an interactive cli to do so.
git rebase --interactive
Change Author Details of All Their Own Commits
To fix an author’s name and/or email in all their commits, we can run the following little shell snippet.
#!/bin/sh git filter-branch --env-filter ' OLD_EMAIL="email@example.com" CORRECT_NAME="Your Correct Name" CORRECT_EMAIL="firstname.lastname@example.org" if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ] then export GIT_COMMITTER_NAME="$CORRECT_NAME" export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL" fi if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ] then export GIT_AUTHOR_NAME="$CORRECT_NAME" export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" fi ' --tag-name-filter cat -- --branches --tags
We often tell our peers, especially juniors that programming style is just a matter of personal preference, and i used to share this sentiment, but the more i think about it the more i realize that this is just bad advice, even tho the compiler might allow you to write braces or spaces in five different styles, that doesn’t mean you should. The language, in combination with the standard library always sets a precedence.