The very basics.

  1. Create your repository on github.com and initialise with a README.md
  2. Clone the repo
  3. Copy files/dirs into cloned repo
  4. update
git clone <repo>.git
cp some_files ~/<repo>
git add -all
git commit -m "Secondary Commit"
git push

n.b. You cannot clone a repo into a repo

as@as-x1:~/MEGAsync/dev/html-css$ git clone https://github.com/shevabam/htmlify-csv.git testing-htmlify-build/
fatal: destination path 'testing-htmlify-build' already exists and is not an empty directory.
Initialise a new Repo and Sync to Remote
git init
git add --all
git commit -am "First Commit"

curl -u 'soulmanos' https://api.github.com/user/repos -d '{"name":"python-mqtt-threaded-class-queue"}'

git remote add origin https://github.com/soulmanos/python-mqtt-threaded-class-queue

git config credential.helper store

git push -u origin master

# Don't think you need this line?
git push --set-upstream python-mqtt-threaded-class-queue master
Merge a branch into master (simple)
git checkout master
git merge namespace-variable 
git status # This will list the action that are required to complete a merge e.g.
add files, or remove files as below: (resolving conflicts)

git add templates/project-request.yml
git rm params/projectrequests/project

git status

# Commit the merge once the conflicts have been resolved as above
git commit -m "Merged namespaces-variables and NetworkPolicies into Master"

# Push to remote
git push -u origin master
Merge a branch into master
git checkout master
git pull
git checkout increased_data_logging
git pull
git rebase -i master
git checkout master
git merge increased_data_logging
Resolve errors

This branch tracking is set up for you automatically when you clone a repository (for the default branch only), but if you add a remote to an existing repository you have to set up the tracking yourself.

git branch --set-upstream-to=origin/master master
fatal: refusing to merge unrelated histories 
git pull origin master --allow-unrelated-histories
Add git Tags (tags(pointers) to commits)
git tag v3.9.0
git push origin --tags
list tags
git tags -l
# Examine repo based on tag
git checkout v3.9.0
# Create a new branch to make changes
git checkout -b <newbranch>
Remove Git Commits from Git History e.g. when you accidentally upload a password or ssh key
Show commit history:
$ git log --oneline
Checkout code to new branch:
$ git checkout --orphan latest_branch

Add all the files to git:
$ git add -A

Commit Files:
$ git commit -am "Cleaned up History"

Show current status to understand where you're working:
$ git status

Show current commits in current branch:
$ git log --oneline

Delete the Master branch - with the history you want to clean up:
$ git branch -D master

Rename the current branch (lastest_branch) to master:
$ git branch -m master

Force update the repository:
$ git push -f origin master

Review current repository status:
$ git branch
$ git status
$ git log --oneline
Alternative method to remove File from git History e.g. Passwords

config/.htpasswd is the file to remove from history

git filter-branch --force --index-filter "git rm --cached --ignore-unmatch config/.htpasswd" --prune-empty --tag-name-filter cat -- --all

Rewrite 4bcd56cc41b685cb10fb10eefbc12e157803b3cc (14/15) (0 seconds passed, remaining 0 predicted)    rm 'config/.htpasswd'
Rewrite 7ea2c91745a2b7187f063bdc591de795432ee1f0 (15/15) (0 seconds passed, remaining 0 predicted)    
Ref 'refs/heads/master' was rewritten
Ref 'refs/remotes/origin/master' was rewritten
Credential Stuff

Cached Credentials

git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'

Save Credentials to store
git config credential.helper store

Create a repository from the cli:
  1. Create the repo
curl -u 'soulmanos' https://api.github.com/user/repos -d '{"name":"rpi-docker-unifi"}'
  1. Clone it to create local directory (avoid having to setup ssh strings with origin/master stuff)
git clone https://github.com/soulmanos/rpi-docker-unifi.git
  1. Add some files
touch app.js README.md
  1. Add newly created files to git manifest
git add --all
  1. Commit those files to 'master'
git commit -m "Initial Commit"
  1. Push the new files to the remote (github) repo
git push -u origin master
Clone someone else's Git & Upload to your own
  1. Create your new blank repo:
curl -u 'soulmanos' https://api.github.com/user/repos -d '{"name":"rpi-docker-pihole"}'
  1. Try and add origin and it'll fail:
$ git remote add origin https://github.com/soulmanos/rpi-docker-pihole.git
fatal: remote origin already exists.
  1. Check to see what the origin is:
$ git remote -v
origin	https://github.com/lp-lab/dockerPiHole.git (fetch)
origin	https://github.com/lp-lab/dockerPiHole.git (push)
  1. Remove origin and check it's removed:
$ git remote rm origin
$ git remote -v
  1. Set new remote 'origin' as your newly created repo:
$ git remote add origin https://github.com/soulmanos/rpi-docker-pihole.git
  1. Push to your new repo:
$ git push -u origin master
More Useful Commands
# Add all files under git directory
git add --all :/

# Create new branch from sub branch
git checkout -b <NewBranchName> <ExistingBranchName>

# Pull down a new branch
git fetch origin solidfire:solidfire

# Start editing that branch of code
git checkout solidfire

# Set upstream tracking to sync remote updates
git branch --set-upstream-to=origin/solidfire solidfire

# Push to the branch you're currently editing
git push -u origin solidfire

# Pull down any remote updates
git pull

# Pull down remote updates from a particular upstream server for a particular branch
git pull DEV dev-branch

# Push to specific branch
git push origin dev-environment

# Check your current working branch
git branch

# Check status of any edited files
git status

# Soft delete a local branch you've created
git branch -d solidfire

# Check remote git status
git remote -v

# Delete a remote branch
git push <remote_name> --delete <branch_name>
git push origin --delete NetworkPolicies

# List all Tracked Files
git ls-tree -r <branch> --name-only

# Grep for tracked files you want to untrack and add to .gitignore
for F in $(git ls-tree -r dev-environment --name-only | grep -i secrets); do echo $F >> .gitignore; done

# Recover Deleted file in Current Branch from master
git checkout master slb_BSS_ProdA_w_secpols.csv

Remove a file from being tracked that was added
# Add the file to the .gitignore file
git rm --cached bashrc.j2 # where bashrc.j2 is a <file>
git add --all
git commit -m "Removed tracked file"
git push -u origin master
Remove a folder from the upstream repo after being added to .gitignore
git rm -r --cached include/
git commit -m 'Remove the now ignored directory include/'
git push origin master
Reset all changes in local repository
git reset --hard
git pull

# Check local & remote dir for same file count
ls -l | wc -l
Diff between previous commits
git log --oneline | head -n 2
git diff 59a990a a1df4c1
Show extra lines in git diff
git diff -U8 9f37d0d
Delete Remote Branch
git branch -d <branch name>
git push origin --delete <branch>

-r -D is hard delete if you've got no unmerged changes, same can be achieved with -f

Sync Multiple Repos
for D in $(find . -mindepth 1 -maxdepth 1 -type d); do cd $D && git pull && cd .. ; done
Rebased and didn't want to?
git reset --hard ORIG_HEAD
Nested Git Repos?
mkdir gitlab_ci_testing
cd $_

git init
touch README.md
git add -A
git commit -m "Initial commit"
git remote add origin https://$PAT@home.soulsignal.com/git/LG/gitlab_ci_testing.git
git branch -m main
git push -u -f origin main

git clone https://$PAT@home.soulsignal.com/gitlab/lg/parent.git
git clone https://$PAT@home.soulsignal.com/gitlab/lg/child_1.git

git add parent/
git add child_1/
git commit -m "Adding nested repos
git push

This is the correct workflow to achieve the result required. i.e. Each directory {parent,child_1} are independent git repos with differing remote upstreams to the base repository. I've attempted this a different way, where I have the repos previously cloned and then git init the base repo. Git attempts to add the repos but complains about the remotes being different, and to add them as submodules, whilst this is entirely possible it's not the desired outcome of the base repo containing all the the nested repos files and folders as raw data

Github Absolute Basics (and now beyond!)