Skip to content

Instantly share code, notes, and snippets.

@lcenchew
Last active October 4, 2022 11:49
Show Gist options
  • Save lcenchew/f1606275503fc7665eb1ad7c356ba2f5 to your computer and use it in GitHub Desktop.
Save lcenchew/f1606275503fc7665eb1ad7c356ba2f5 to your computer and use it in GitHub Desktop.
Notes - Git #notes

Git

🔽

Setting up

generate key for access

ssh-keygen -t rsa -b 4096 -f github-id_rsa -C "Github"
mv github-id_rsa* ~/.ssh/

ssh-keygen -t ED25519 -f gitlab-id_ED25519 -C "Gitlab"
mv gitlab-id_ED25519* ~/.ssh/

Add to .ssh/config

Host github.com
  Preferredauthentications publickey
  IdentityFile ~/.ssh/github-id_rsa

Set email & name

git config user.email "first.last@example.com"
git config user.name "first last"

Fix old mode, new mode changes

git config core.filemode false

Using

Commit without message

git commit -a --allow-empty-message -m ""

Adding upstream source

git remote add -f --no-tags -t master -m master upstream <repo>
git fetch upsteam

Checkout another remote

git fetch remote2
git checkout -b remote2-branch remote2/branch

Rewriting History

Delete branch

git branch -d localBranchName
git push origin --delete remoteBranchName

Delete pushed commit

git push -f origin last_known_good_commit:branch_name	

Deleting a pushed commit in the middle

master branch: 
5c20bfa Commit 4
f0c7d8d Commit 3 - to delete
1463166 Commit 2 - to delete
9e0f65d Commit 1 
31a0bbd Initial

git checkout 9e0f65d
git branch tmp
git checkout tmp
git cherry-pick 5c20bfa
git checkout master
git reset --hard 9e0f65d
git merge tmp
git push -f master

Remove sensitive file https://stackoverflow.com/questions/872565/remove-sensitive-files-and-their-commits-from-git-history

Scripts

Pull all the repositories in a folder

find . \
    -maxdepth 2 -type d \
    -name ".git" \
    -execdir python -c 'import os; print(os.path.abspath("."))' \; \
    -execdir git branch \; \
    -execdir git pull \;

Tools

brew cask install sourcetree

Github

Clone a gist (this repo)

git clone git@gist.github.com:f1606275503fc7665eb1ad7c356ba2f5.git

Note: Gist does not support folder

Gitlab

Gitpod

Setup (Github)

  1. Open with https://gitpod.io/#https://github.com/user/repo
  2. Add .gitpod.Dockerfile, .gitpod.yml

Installing VS Code extension

Containers

codes

#!/bin/bash
# Clone fork and add upstream as remote
echo "Usage: $0 <repo-name>"
if [ -n "$1" ]; then
git clone git@github.com:my-username/$1.git
cd $1
git remote add upstream git@github.com:my-upstream/$1.git
git fetch upstream
git checkout master
git merge upstream/master
git push
fi
#!/bin/bash
# Make a copy of the repo without .git
echo "Usage: $0 <repo-name>"
if [ -n "$1" ]; then
git clone --depth=1 git@github.com:my-username/$1.git
rm -rf ./$1/.git
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment