Skip to content

Instantly share code, notes, and snippets.

@Rampoina
Last active August 29, 2015 14:05
Show Gist options
  • Save Rampoina/767e83606476859d2c08 to your computer and use it in GitHub Desktop.
Save Rampoina/767e83606476859d2c08 to your computer and use it in GitHub Desktop.
Git tips

Oops, I forgot to add some files to my last commit!

Don't worry, instead of creating a new commit with the new files:

git add . // add new files
git commit -m "Oops I forgot some files sry"

You can fix your last commit!:

git add . //add new files 
git commit --ammend //commit with the ammend flag  

Oops, I added some files to my last commit that don't belong there!

Again, instead of creating a new commit deleting the files:

git rm . // remove files
git commit -m "Oops those files shouldn't be here sry"

You can also fix your last commit!:

git --reset soft HEAD~1 //go back one commit
git add . //add the files again
git commit //and commit again

Oops, I should have created a branch x commits ago!

create and checkout your new branch, and then create your old branch x commits ago (with the -f flag because it already exists, effectively moving it)

git checkout -b newbranch               //create and switch to the new branch
git branch -f originalbranch HEAD~x     //create the original again x commits ago

Separate whitespace changes (including indentation) from your real changes

Edit ~/.gitconfig and add the following alias:

[alias]
    addw = !sh -c 'git diff -w --no-color "$@" | git apply --cached --ignore-whitespace' -

Then instead of using git add to stage the changes use git addw:

git addw .
git commit -m "Changes"

When you're done, the whitespace changes are unstaged so you can add and commit them:

git add .
git commit -m "Whitespace changes"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment