• courses
  • egghead
  • git

Fix Common Git Mistakes

Stuff I learned from the Egghead course Fix Common Git Mistakes

  1. git commit --amend -m "new commit message"

To fix the commit message (before pushing). This also works if you want to add more files to a commit.

  1. git log --oneline

Display logs in one line.

  1. git reset HEAD [filename]

To unstage a specific file.

  1. git diff origin/master HEAD

To see the difference between the currrent working directoy and master.

  1. git reset [commit hash] OR git reset HEAD~1

(for one commit back)

It's recommended that you use reset only on branches you haven't pushed yet.

  1. git reset --soft

Undoes the commit, but changes are still staged.

  1. git reset --mixed OR git reset

Removes the commit and unstages the changes.

  1. git reset --hard

Removes the commit, unstages the stages and removes them from the working directory.

  1. git reflog

To see all the changes done in your git repository.

  1. git reset --hard [commit hash]

You will reset to a speficic commit.

  1. git revert [commit hash]

You shouldn't reset commits you've already pushed (that's changing history), instead do a revert.

  1. git branch -vv

Verbose output.

  1. git push --set-upstream origin my-branch

Equal to: git push -u origin my-branch

  1. git cherry-pick [commit hash]

For copying a commit from one branch to another.

  1. git stash pop AND git stash apply

To apply the changes from the stash (apply keeps the changes in the stash). With git stash pop the changes normally go away (but not in case of a merge conflict)

  1. git stash drop [commit hash]

To remove changes from a stash.

  1. git checkout -b branch

If you want to explore a commit, but get rid of the dettached HEAD state, you can checkout the commit as it's own branch.

  1. git remote prune origin --dry-run

Check which branches will be disassociated with the master locally after you've merged them into the remote master. Then run: git remote prune origin and afterwards: git branch -d [branch name]

  1. git rebase -i HEAD~3

To interactively rebase the last three commits.

  1. git rm -r --cached .

Remove all files from the git repository. Afterward run git add . to add them all back in (this is for applying the gitignore file to the files that have been added before gitignore).

  1. git rebase -i HEAD~2

To add a file to a previous commit get into the interactive rebase mode, then choose edit for that specific commit. Do your changes, then do git commit --amend --no-edit (to keep the same commit message) and then git rebase --continue.

  1. git rebase -i HEAD~2

For squashing commits.

  1. For completely removing a file from the github history.