My most commonly used git commands

Initialize a git repository in a directory

git init

Track new files by adding them to the staging area (files that are ready to be committed)

git add README.md # tracks a single file
git add -A # track all modified files

Check the status of files

git status

Commit files in the staging area, that is, add logs for tracked files

git commit -m "add README" # commit with a message

Show history of HEAD

git reflog show

Reset to an older commit

git reset --hard 0d1d7fc32 # --hard will discard all local changes
git reset --keep HEAD@{0} # --keep is safer than --hard

Read logs

git log

Push your changes to a remote branch

git push # if default remote has been set
git push <remote> <branch> # if default remote has not been set

Undo

Remove a file from staging

git reset <file>

Reset all files against a branch

git reset --soft <branch> # undo commits but keep all files in staging
git reset HEAD^ # undo commits and remove all files from staging
git reset --hard <branch> # undo all changes since last commit on a specific branch

Remove file(s) and/or folder(s) from staging

git clean -df # clear all unstaged files
git reset HEAD <file> # remove a single file or folder from staging
git reset HEAD . # from every file and folder from staging
git reset --hard # remove all staged and unstaged files

Remove stray file(s) and/or folder(s) after they've been committed (this is dangerous) https://stackoverflow.com/a/21171527/3260090

git rm --cached README.md # remove a single file
git rm -r --cached sample-directory # remove directory and its content

Undo a git pull

git reset --hard HEAD^

Stashes

Stash your changes

git stash
git stash save "stash README.md" # create a stash log with a comment

Grab your latest stash

git stash apply

Grab your latest stash and then removes it from the stash list

git stash pop

List all stashes

git stash list

Get a specific stash

git stash apply stash@{3}

Drop the latest stash

git stash drop

Drop a specific stash

git stash drop stash@{2}

Clear all stashes

git stash clear

Checkouts

Create and switch to a new branch

git checkout -b "feat-hover"

Checkout an older commit

git checkout 0d1d7fc32

Checkout an older commit and make changes there

git checkout -b old-state 0d1d7fc32

Remotes

List remotes

git remote

Add remote

git remote add origin <url>

Add a remote for an upstream repo (the main repo you forked from)

git remote add upstream <url>

Rebase

Modify a specific commit

go to a specific commit

git rebase --interactive 'bbc643cd^'

commit with the same message

git commit --all --amend --no-edit

return to the previous head commit

git rebase --continue

References

https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-commits-in-git https://stackoverflow.com/questions/8358035/whats-the-difference-between-git-revert-checkout-and-reset https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History https://stackoverflow.com/questions/5798930/git-rm-cached-x-vs-git-reset-head-x https://medium.com/datadriveninvestor/git-rebase-vs-merge-cc5199edd77c