Lauro SilvaLauro Silva

These notes are intended to be used and studied in tandem with Chris Achard’s course.

Original Repo: https://github.com/laurosilvacom/fix-common-git-mistakes-notes

If you notice areas that could be improved please feel free to open a PR!

Course Summary

  • Change commit messages
  • Add or remove files from a commit
  • How and when to stash changes
  • What a “detached HEAD” is, and how to fix it
  • Remove secrets from a codebase
  • How to rewrite history

Original Code Snippets

1. Change a Commit Message that Hasn’t Been Pushed Yet

1git commit --amend -m "New message"

2. Add More Files and Changes to a Commit Before Pushing

1# only do this BEFORE you've pushed the commits
2git add -A
3
4git commit --amend -m "My new message"

3. Remove Files from Staging Before Committing

1git reset HEAD filename

4. Remove Changes from a Commit Before Pushing

1# reset back to a specific commit
2git reset HEAD~1
3# or
4git reset [HASH]

5. The Different git Reset Options: —hard, —soft, and —mixed

1# Resets the index and working tree
2git reset --hard [commit]
3
4# Does not touch the index file or the working tree at all
5git reset --soft [commit]
6
7# Resets the index but not the working tree
8# Reports what has not been updated
9# This is the default action
10git reset --mixed [commit]

6. Recover Local Changes from git reset --hard with git reflog

1# To look up the commit hash
2git reflog
3
4git reset --hard [HASH]

7. Undo a Commit that has Already Been Pushed

1# NOTE: Once a commit is pushed, you do NOT want to use git reset
2# make a "revert commit" which will "undo" a specific commit
3git revert [HASH-TO-UNDO]

8. Push a New Branch to GitHub that Doesn’t Exist Remotely Yet

1git checkout -b new-branch
2
3git push
4
5# set the upstream of the local branch at the same time
6git push --set-upstream origin new-branch

9. Copy a Commit from One Branch to Another

1git cherry-pick [HASH-TO-MOVE]

10. Move a Commit that was Committed on the Wrong Branch

1# Get the commit we want
2git cherry-pick [HASH-TO-MOVE]
3
4# Remove the commit from the wrong branch
5git reset [HASH-TO-REMOVE]

11. Use git stash to Save Local Changes While Pulling

1# Save the local changes,
2git stash
3
4# Get remote changes
5git pull
6
7# To apply the stashed changed
8git stash pop
9
10# You will need to fix the merge conflict
11# Then drop the change from the stash
12git stash drop stash@{0}

12. Explore Old Commits with a Detached HEAD, and then Recover

1# checkout the hash of an old commit
2git checkout [HASH]
3
4# we'll be in a "detached HEAD" state
5# Save the work by creating a new branch
6git checkout -b my-new-branch

13. Fix a Pull Request that has a Merge Conflict

1git checkout -b conflicts_branch
2
3# Add 'Line4' and 'Line5'
4
5git commit -am "add line4 and line5"
6git push origin conflicts_branch
7
8git checkout master
9
10# Add 'Line6' and 'Line7'`
11git commit -am "add line6 and line7"
12git push origin master

14.Cleanup and Delete Branches After a Pull Request

1# use the github interface to delete the branch remotely
2
3# Locally
4# Confirm that remote is gone
5git remote prune origin --dry-run
6git remote prune origin
7
8#clean up the feature branch
9git branch -d feature-branch

15. Change the Commit Message of a Previous Commit with Interactive Rebase

1git log --oneline
2
3# start the interactive rebase
4git rebase -i HEAD~3
5# and then change pick to reword.
6# We can now reword the commit message

16. git Ignore a File that has Already been Committed and Pushed

1# We make a file and accidentally push it to github
2# To remove it, add it to .gitignore file
3# remove all of our files from our git cache
4git rm -r --cached .
5
6# add back all the files we want with
7git add -A

17. Add a File to a Previous Commit with Interactive Rebase

1git rebase -i HEAD~2
2
3# during the interactive rebase, we can add the file, and amend the commi
4git commit --amend --no-edit
5
6git rebase --continue

18. Fix Merge Conflicts While Changing Commits During an Interactive Rebase

1# ennter interactive rebase
2git rebase -i HEAD~2
3
4# Then we can fix that merge conflict like normal, but finish up the rebase
5git rebase --continue

19. Squash Commits Before they are Pushed with Interactive Rebase

1git rebase -i HEAD~3
2
3# Make the changes in interactive rebase
4# Make the commit message for that commit, and once we save the message
5# we'll be left with just a single commit

20. Completely Remove a File from Pushed git History

1# prune the entire history and garbage collect the remains
2git reflog expire --expire=now --all && git gc --prune=now --aggressive
3
4# use git push to push that change to github,
5# and remove the .env file from all of the history

© Lauro Silva, LLC. All rights reserved.