Uncommited Local Changes
1. revert local changes
git restore fname
2. accidentally deleted a file
git restore fname
3. discarding chunks/lines (-p = patch)
# interactive, y/n for discard
git restore -p fname
4. discard all local changes
git restore .
(git stash)
Looking at committed history !! never change history that has been pushed
feature branch before adding to a team branch
5. fixing last commit message
git commit --amend -m "better commit message"
6. adding file to commit
git commit --amend (used tower to add file)
7. reverting a commit in the middle (c1, c2, c3; c2 is bad)
git revert bad-commit-hash
8. resetting to an old revision (everything after c2 is bad)
git reset --hard good-commit-hash
(--hard and --mixed are all you need; --mixed keeps changes as local changes)
9. resetting a file to an old revision
## returns to prior state for a specific file
git restore --source good-hash fname
## reflog -- journal of head pointer changes
10. recover a deleted revision
git reflog
-- sorted chronologically
-- get accidentally deleted
git branch happy-ending previous-state
11. recovering a deleted branch
git reflog (note we had to switch away from branch)
git branch feature/login earlier-hash
12. moving a commit to a new branch
-- supposed committed to master instead of feature branch
-- we see most recent commit on master
(drag commit from master to feature branch; delete commit on master)
git branch feature/login
git reset HEAD~1 --hard
13. moving a commit to a different branch (which exists)
-- use cherry pick to move commit to correct branch
-- clean up master
git checkout feature/newsletter
git cherry-pick sha
git checkout master
git reset --hard HEAD~1
interactive rebase (use for only local history!!!)
14. edit old commit
git rebase -i HEAD~3
-- commits in reverse order
-- go to parent commit of commit you want to change
action keyword: reword
-- git opens editor for commit message
15. delete old commits
git rebase -i HEAD~2 (or copy commit hash)
action keyword: drop
16. squashing multiple commits into one
git rebase -i HEAD~3
action keyword: squash
-- line marked squash is combined with line above
-- get edit for this squashing commit
17. adding a change (file) to an old commit
-- c4 has fix for c2
-- c4 is good
-- two steps: use fixup to commit, rebase autosquash
git add missing-file
git commit --fixup broken-hash
git rebase -i HEAD~4 --autosquash
-- changes order of bandaid commit
18. splitting/editing old commit
(in gui)
git-tower.com/learn
git-tower.com/help