git rm --cached; git add; git status

Stop tracking a file. Cached option removes it from the git repo but keeps it in the working directory.

git revert --no-commit <hash>..HEAD

This walks back every commit from the HEAD until and not including <hash>. or you can do

git revert --no-commit HEAD~N..HEAD
git revert --quit

or do

git reset --soft HEAD~N

to just pop N commits

git checkout <branch>
git reset --hard <branch>

if you want to get rid of all your local changes on <branch>

git push/pull -u <repo_name> <branch_name>

e.g git pull -u origin prod to pull the origin/prod branch into this local branch that i’ve checked out

git config --local core.hooksPath .git/hooks

Full reset a repo back to it’s origin, removing untracked shit do

git reset --hard origin/main
git add forgotten-file.txt
git commit --amend --no-edit

--amend edits the commit at the tip. just –amend and -m would edit the message

how do I restore a file to before I ever touched it?

git restore --source=<commit_hash>~1 -- <files>

so i ran some automated tool only to find after a few commits that it’s messed up a line or two in a few files around the place. conveniently these erroneous changes were all part of the same initial commit so I 1) found that commit hash 2) listed all the different files after the – optional arguments. note the tilda 1 indicating that we’re restoring each of those listed files to 1 before the commit id, if we didn’t include that part then it wouldn’t change those files (as I only modified them as part of that one commit id)

how do i remove changes from a branch?

small change?

git reset --hard origin
<re-do work here lol>
git push --force (if origin is already set)

I could’ve created a new branch that is a different name, cherry pick the changes from the polluted branch:

git log # copy the commit hash you want from the old branch
git switch -C <new_branch> origin/master
git cherry-pick <commit_hash>

how to create a new branch from main?

git switch -C <new-branch-name> <main-branch>

-C flag forces the create from start point <main-branch>

how to merge changes to master branch?

i’ve already made changes without pulling in remotes

git add
git commit

then

git switch -c <new_branch_name>
git pull origin <main_branch_name>
git merge <main_branch_name> # remove if this wasn't necessary

if you have conflicts you need to go to each conflicted file in a text editor and remove the lines you don’t want (… and keep the ones you want to keep) e.g.

>>>> local current changes/outgoing
blah blah
======
blah
<<<< remote current changes/incoming

this is why you git pull origin master before you make any changes

gh pr create

i haven’t made changes

great

git switch -c <new_branch>
git pull origin master

make changes

git add
git commit
gh pr create

how do i clone a repo at a certain tag?

git clone --depth 1 --branch <tag_name> <repo_url>