Setup and use a new online repo

Sign in to github or bitbucket; start a new repository; add .gitignore (things to exclude such as your python virtualenv, config files with passwords etc), then follow the instructions to:

git clone # Clone the remote repo to local repo
git add -A # Add all files / subdirectories to staging area
git commit -m 'Commit comment' # Commit the changes to the local repo
git push -u origin master # Push to the remote repo, master branch

Setup new online repo from command line

git init
git add .
git commit -m 'initial commit of full repository'
# bitbucket:
curl --user USERNAME https://api.bitbucket.org/1.0/repositories/ \
--data name=REPONAME --data is_private='true'
git remote add origin https://USERNAME@bitbucket.org/USERNAME/REPONAME.git
# github:
curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPONAME "}'
git remote add origin https://github.com/andy-pi/test.git
git push -u origin --all

Find out what is going on

git status # Get current status of git
git log # Get Git commit log showing their SHA
git branch # List local branches and find out current branch
git diff # Shows differences (?)

Using branches

git branch branch_name # Creates a new branch.
git checkout branch_name # Used to switch from one branch to another.
git merge branch_name # Used to join file changes from one branch to another.
git branch -d branch_name # Deletes the branch specified.

To merge back to master

git checkout master # Switch back to master branch.
git merge branch_name # Merge the branch.
git push # push the master branch
git branch -d branch_name # Deletes the local branch specified.
git push origin --delete branch_name # Deletes the remote branch

To obliterate local changes / mistakes and get back to last commit on remote

git fetch -all # Fetches all remote repo, but doesn't apply it
git reset --hard origin/<branch_name> # Merge the branch.

To merge remote changes in when you also have uncommited local changes

git stash --include-untracked # stashes uncommited local changes to a temp location
git pull # gets latest changes from the remote repo
git stash pop # overwrites current workspace with contents of the stash; drops the stash
# if remote changes cause conflicts with the stash, these need to be resolved manually
(git stash list)
(git stash clear)

To merge remote changes when you also have ALREADY commited local changes
(https://www.freecodecamp.org/news/git-pull-force-how-to-overwrite-local-changes-with-git/)

git pull # combination of git fetch and git merge
# if remote changes cause merge conflicts, these need to be resolved manually, then:
git add .
git commit -m "merge message"
git push

Undoing / resetting / updating stuff

git checkout HEAD filename # Discards changes in the working directory.
git reset HEAD filename # Unstages file changes in the staging area.
git reset # Can be used to reset to a previous commit.
git fetch # fetches the remote repo and updates local copy
git rm --cached filename # remove file from the repo without deleting local copy of the file
git rm filename.txt # remove file from the repo completely
git commit -m "remove filename.txt"

Remove all commits before commit XXXXX

git checkout --orphan temp XXXXX
git commit -m "Truncated history"
git rebase --onto temp XXXXXmaster
git branch -D temp
git prune --progress
git gc --aggressive
git push -f origin master