Docs
General
Git

My Cheatsheet

Userful Commands

# list all branches
git branch
 
# view git history
git reflog
 
# files changed
git diff
 
# commit list
git log
 
# staged and untracked files
git status
 
# edit local commit message
git commit --amend
 
# view old commits, opens in detached branch
git checkout <hash>
# create a new branch
git branch <name>
 
# changes of file since last commit
git diff HEAD ./path/to/file
 
# destroy tracked & unstaged file changes
git checkout .
 
# created untracked & unstaged files are removed (-d: dirs / -f: files)
git clean -df
 
# unstages files, changes untouched
git reset
 
# destroy all changes of tracked file
git restore <file-name>
 
# unstages files, changes untouched
git restore --staged .
 
# destroy commit, commit changes staged, untracked files untouched, <hash> commit isn't affected
git reset --soft <hash>
 
# destroy commit, commit changes and previous staged and untracked files untouched, <hash> commit isn't affected
git reset <hash>
 
# destroy commit and previous staged files, reverts tracked files to <hash> commit, untracked files untouched
git reset --hard <hash>
 
# add commit on top, specifically destroying given commit hash changes, untracked files untouched
git revert <hash>
# deleting all changes since last commit
git reset          # unstage everything
git checkout .     # reset tracked files
git clean -dfx     # get rid of untracked files
 
# restore old commit to new branch
git checkout <hash>
git branch <branch-name>

Gitignore

The below excludes all files except the files inside of and the /vendor/laravel/ui dir itself. It's mainly about ignoring files in a directory. Then not-ignoring selected directories inside of it. Source (opens in a new tab)

Remember /vendor/* != /vendor/. The former keeps the /vendor/ folder but ignores files inside of it. The latter ignores the whole /vendor/ directory.

# ignore everything inside /vendor/ but not dir itself
/vendor/*
 
# don't ignore /vendor/laravel dir and files inside of it
!/vendor/laravel/
 
# ignore every file inside /vendor/laravel dir, but not dir itself
/vendor/laravel/*
 
# don't ignore /vendor/laravel/ui dir and files inside of it
!/vendor/laravel/ui

Existing Project to Git

Source (opens in a new tab)

# first init
git init
 
# stage local changes
git add .
 
# commit local changes
git commit -m 'initial'
 
# connect to origin, use url from github repo
git remote add origin <url>
git remote -v
 
# finally push to origin, check origin name (master/main) from github
git push origin <origin-name>
 
# set default origin branch to master
git push --set-upstream origin master

Local SSH

To generate ssh keys, change key-name and /absolute/path

ssh-keygen -t rsa -b 4096 -C "key-name" -f "/absolute/path"

Check if ssh-agent is running

eval "$(ssh-agent -s)"

Add generated ssh key to ssh-agent

ssh-add "/key/location"

Then just copy the .pub and paste it into github.com. Run this to check if the key works.

ssh -T git@github.com
 
> Hi <username>! You've successfully authenticated, but GitHub does not
> provide shell access.

Submodules

To put your submodule code back into the main repository, you just need to remove the submodule and re-add the files into the main repo:

# delete reference to submodule HEAD (no trailing slash)
git rm --cached submodule_path
 
# if more than one submodule in repo, edit, don't delete
git rm .gitmodules
rm -rf submodule_path/.git
 
# add then commit the newly unmoduled files
git add .
git commit -m "remove submodule"

Source (opens in a new tab)

Automated Deployment

There are multiple ways to automate deployment. See the pros and cons of each of these methods here (opens in a new tab). For more ways to do this check out this cheatsheet (opens in a new tab)

Deploy Keys

This is very similar to local development with the usual ssh keys. The only difference is in how much this key gets to do. It's access is limited only to the repo it's added to.

Create a key pair and add it here Repo > Settings > Deploy Keys

ssh-keygen -t rsa -b 4096 -C "key-name" -f "/abs/path"

Using deploy keys in scripts. The .ask-pass should echo the ssh key password.

# start ssh-agent
eval "$(ssh-agent -s)"
 
# add the ssh key
DISPLAY=:0 SSH_ASKPASS="/abs/path/.ask-pass" ssh-add ~/.ssh/<key-name>
 
# do stuff
 
# kill ssh-agent
trap "ssh-agent -k" exit

Always remember to kill what you start