Skip to content

Git Cheat Sheet

Setup SSH

  1. Run the command below:
bash
ssh-keygen -t ed25519 -C "<comment>"
  1. You will be asked for the key location and passphrase. Press Enter to use the defaults if needed.
txt
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:
  1. Copy the public key and add it to your Git hosting provider.

Initialize

bash
# initialise git on this folder
git init

# Init branch
git init --initial-branch=main

# If you want to use a different name per project
git config user.name "<your_user_name>"
git config user.email "<your_user_email>"
# Or set it globally so it is shared across all projects
git config --global user.name "<user name>"
git config --global user.email "<email>"

# If you need to use a different key, either
git config sshCommand ssh -i C:/Path/key
# Or add the line below in .git/config
# sshCommand = ssh -i C:/Path/key

# Link this folder with a git repository
git remote add origin <git@git_url>
git pull

# If there is no branch
git pull origin master

# To work on a branch
git checkout <branch name>

Cheat Sheet

Frequent 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 a file or folder to the staging area
git add <file name | folder name>

# commit staged changes with a useful message
git commit <file name | folder name> -m "<commit comments>"

# amend the last commit message
git commit --amend -m "<new comment>"

# temporarily store the changes and revert
git stash -m "<comment>"

# apply the 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 an 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 the local branch to a remote branch with a different name
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

# list all merges
git reflog

# revert to merge id
git reset --merge <id>

# git worktree 
git worktree add <--checkout> <path> <source>

# show diff
git diff --unified=1000 <filename>

# ignore line endings
git config --global core.autocrlf true

# Add tag
git tag <tag name> -m <comment>

# Push tag
git push origin <tag name>

# Delete tag
git tag -d <tag name>

.gitignore

A file to store files and folders that should be ignored by git.

Example .gitignore content:

txt
node_modules
.vscode
testing.js

User-level git ignore:

You can add a personal git ignore file that is not committed to the repository.

bash
git config [--global] core.excludesfile <dir>/.gitignore

Create a 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

  1. Go to the git hook folder.
bash
cd test.git
cd hooks
cp post-update.sample post-update
  1. Change the content of post-update to:
sh
cd ~/test && git --git-dir ~/test/.git pull