Skip to content

Git

Also check out Git

If you are just starting answer the following questions in a text document by searching and watching YouTube videos

  • How do I install git
  • How do I set name and email
    • How to I update name and email
  • How do I clone a git repo
    • How do I use Git via HTTPS
    • How do I use Git via SSH
    • What is the pro and con to using Git via HTTPS verses SSH
  • How do I add and stage files in order to make a commit
    • How can I add individual files
    • How can I add all files in a directory
  • What is git pull and what does it do
  • How do I make a commit
  • How do I make a branch
  • How do I list the branches I have
  • How do I change branches
  • How do I merge a branch*
    • This one can be complicated, just learn the basics
  • What is git push
    • How to I select which branch to push to
    • How do I which remote repository to push to
  • What is a Pull Request
    • Where do I find pull requests?
    • How do I submit a pull request

Reset name and email

git config --global -e

Reset branch to head and delete unused files

git reset --hard HEAD
# Delete all unused files
git clean
git clean -f

How to remove blob hash from history?

git ls-tree -r HEAD

export BLOB_HASH=22bd3c3816d7169479dde2ce7cf0d6571c63cb89b52b8f730412457be11e8a61
git filter-branch \
    --force \
    --index-filter \
    "git ls-files -cdmo -s | grep '$BLOB_HASH' | awk '{print $4}' | xargs git rm --cached --ignore-unmatch 656565randomstring546464" --prune-empty --tag-name-filter cat -- --all


Force Git Push

git push origin <your_branch_name> --force

Revert history of remote Git repository

git reset --hard commit_hash
git push https://git.... --force

Change text editor

# Change text editor
git config --global core.editor "nano"
git config --global core.editor "vim"

Changing Names of Branches

Changing a remote branch name

# list all remote branches
git fetch
git branch -a

# Changing a Remote Branch
git remote -v
git remote add <remote_name> <remote_url>
git remote rename <old_name> <new_name>
git remote remove <name>

Delete local and remote branch separately

# local
git branch -d feature/login
# remote
git push origin --delete feature/login

Delete a local and a remote GIT branch

git branch -D branch_name
git push <remote_name> --delete <branch_name>

Advanced Tricks


# Skip `git add .`
git commit -am "git commit message"

# Check git config
git config -e
git config --global -e

# Local git config

# Change previous commit before push
git commit --ammend
git commit --ammend --no-edit

# Force local code to remote branch
git push origin master --force

# Go back to previous commit but keep history
git revert <branch or commit hash>

# Save stuff for later locally without creating a separate branch
git stash
git stash pop
git stash save stashname
git stash list
git stash apply stashname

# Rename a branch
git branch -M new_name

# Better git log
git log --graph --oneline --decorate

# How do I got back one commit at a time without having to checkout
git bisect start 
# bad
# good

# How do I compress commits
git rebase master --interactive
# popup, replace pick with `squash` to keep comment in logs or `fixup` to remove comment for what you want to merge into a single commit
git commit --fixup  commit_hash
git commit --squash commit_hash
git rebase -i --autosquash

# git hooks, scripts trigged by git
# inside .git/hooks
# https://www.npmjs.com/package/husky

# Fuck my code and reset everything
git reset --hard HEAD
git clean -df

# Checkout previous branch
git checkout -

Git submodules

# Add
git submodule add {ssh or https path to git repo}}
# pull / Update
git submodule update --recursive --remote

Pull all branches

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

Sources

Git Research

Initializing Keybase... done.
Syncing with Keybase... done.
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

git commit --date="1 hours ago" -m "message"

git commit --amend --date="Jan 24 22:02 2023 +0500" --no-edit

wiki.software.List.ssh

Remote Branches

# list all remote branches
git fetch
git branch -a

https://tecadmin.net/list-all-remote-branches-in-git/

Rename local and remote

https://multiplestates.wordpress.com/2015/02/05/rename-a-local-and-remote-branch-in-git/

Delete local and remote branch separately

# local
git branch -d feature/login
# remote
git push origin --delete feature/login

Pull all branches

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

Delete a local and a remote GIT branch

git submodules

# Add
git submodule add {ssh or https path to git repo}}
# pull / Update
git submodule update --recursive --remote

Restore a delete file

Delete file form hit history

git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD

Reset branch to head

git reset --hard HEAD
# Delete all unused files
git clean
git clean -f

Delete untracked files

git clean -n
git clean -f

git pull fails "unable to resolve reference" "unable to update local

https://stackoverflow.com/questions/2998832/git-pull-fails-unable-to-resolve-reference-unable-to-update-local-ref/30939030

Revert to previous commit

git reset --hard COMMIT HASH
git push https://git.... --force

For push

git push origin <your_branch_name> --force

Amend

npm install -g ts-node typescript '@types/node'

npx ts-node typescript-file.ts