Git Cheet Sheet
Setup ssh
- Enter command below:
bash
ssh-keygen -t ed25519 -C "<comment>"
- It will request user to specified the directory of the key and password, you can press ENTER for default.
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
- Copy content of the public key and put in git service provider page
Initialize
bash
# initialise git on this folder
git init
# Init branch
git init --initial-branch=main
# If you want to use different name per project
git config user.name "<your_user_name>"
git config user.email "<your_user_email>"
# Or set as global and it shared across all projects
git config --global user.name "<user name>"
git config --global user.email "<email>"
# If need to use different key, either
git config sshCommand ssh -i C:/Path/key
# Or add line below at .git/config
# sshCommand = ssh -i C:/Path/key
# Link this folder with git repository
git remote add origin <git@git_url>
git pull
# If there is no branch
git pull origin master
# To do development on a branch,
git checkout <branch name>
Cheatsheet
Frequest use commands
bash
# switch branch
git checkout <branch>
# push to git
git push
# pull from git repository
git pull
# pull master from origin
git pull origin master
# add file to folder to staging area
git add <file name | folder name>
# commit file(s) or folder(s) at staging area, put in useful comment for easy tracking in future
git commit <file name | folder name> -m "<commit comments>"
# amend the last commit comment
git commit --amend -m "<new comment>"
# temporary store the changes and revert
git stash -m "<comment>"
# apply last stored changes
git stash pop
# push to a destination
git push <destination> <source>:<origin>
# merge branch
git merge <branch>
# merge branch without commit and fast forward
git merge <branch> --no-commit --no-ff
# get file from another branch
git checkout <revision> -- <filename>
# rename remote
git remote rename <from name> <to name>
# remove accidentally pushed file from git
git rm --cached <filename>
# reset to previous node
git reset --hard <node>
# apply commit from a node
git cherry-pick <node>
git merge --squash
# abort current merge
git merge --abort
# point git local branch to remote different branch name
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
# list all the merge
git reflog
# revert to merge id
git reset --merge <id>
# git worktree
git worktree add <--checkout> <path> <source>
# show different
git diff --unified=1000 <filename>
# ignore line endings
git config --global core.autocrlf true
.gitingore
A file to store all file(s), folder(s) that will be ignore by git Example of the .gitignore
content:
node_modules
.vscode
testing.js
User level git ingore You can add your personel git ignore that not commit to git repository
bash
git config [--global] core.excludesfile <dir>/.gitignore
Create Git Server
User side
bash
mkdir .ssh
chmod 700 .ssh
cd .ssh/
touch authorized_keys
chmod 600 authorized_keys
ssh-keygen -t dsa
vi authorized_keys
Server Side
bash
mkdir server.git
cd server.git/
git init --bare
cd ..
mkdir server
cd server
git clone -s -- ../server.git ./
Client side
bash
mkdir test
cd test
git init --initial-branch=master
git config user.name "username"
git config user.email "email"
git config core.sshCommand "ssh -i private-key.file"
git remote add test user@server-name:test.git
git push test master
Configure hook
- Go to git hook folder.
bash
cd test.git
cd hooks
cp post-update.sample post-update
2.Change the content of the post-update to
sh
cd ~/test && git --git-dir ~/test/.git pull