Git-Version control system
On this page
- Flight rules for Git
- Tips
- Tools
- Misc
- Check Git config
- Change Git Remote URL
- Undoing Mistakes
- Rebase
- Recover from git reset --hard
- Count all tracked files in a git respository
- Discard user email "xxxx" related commits after the specific commit
- Checkout files/folders from a specific branch
- Branches
- Stashes
- Worktree
- Tags
- Alias
- Sources
- Flight rules for Git
- Tips
- Tools
- Misc
- Check Git config
- Change Git Remote URL
- Undoing Mistakes
- Rebase
- Recover from git reset --hard
- Count all tracked files in a git respository
- Discard user email "xxxx" related commits after the specific commit
- Checkout files/folders from a specific branch
- Branches
- Stashes
- Worktree
- Tags
- Alias
- Sources
Flight rules for Git
Flight rules for git that list, step-by-step, what to do if X occurs, and why. Essentially, they are extremely detailed, scenario-specific standard operating procedures.
Tips
.gitkeep
: a dummy file to enable git to track a completely empty directory
Tools

GitHub style split diffs with syntax highlighting in your terminal.
- gum: manage multiple git user config
yarn global add @gauseen/gumgum listgum set user1 --name 'user 1' --email user1@email.comgum use user1#orgum use user1 --global
Misc
# Discards all unstaged changesgit reset --hard HEAD# Throw away local modificationsgit checkout -f
Check Git config
git config --listgit config user.namegit config user.email
Change Git Remote URL
git remote set-url origin git@github.com-lkcozy:lkcozy/code-notes.git
Undoing Mistakes
# Discard Uncommitted Changes In A Filegit restore index.html# Fix The Very Last Commitgit commit --amend -m "A message without typos"# forgotten to add a certain changegit add forgotten-change.txtgit commit --amend --no-edit# Recover Lost Commits Using The Refloggit refloggit branch happy-ending e5b19e4# Restore A Single File From A Previous Stategit log -- <filename>git checkout <deletion commit hash>~1 -- <filename>
Rebase
# amending your last commitgit commit -a --amend
# fixing up older commits, -i for interactivegit rebase -i HEAD~3
When should I use git pull --rebase?
Point 1
Use git pull --rebase
only if you know you forgot to push your commits before someone else does the same.
If you did not commit anything, but your working space is not clean, just git stash before to git pull. This way you won't silently rewrite your history (which could silently drop some of your work).
Point 2
I think you should use git pull --rebase
when collaborating with others on the same branch, which will avoid the extra merge commits.
Don't git pull, use git pull --rebase instead

git pull = git fetch + git merge FETCH_HEADgit pull --rebase = git fetch + git rebase FETCH_HEAD
Git pull --rebase
effectively reapplying our local changes on top of the remote changes, resulting in a tidy, linear commit history.
Tip #1: Use git config --global pull.rebase true to avoid typing the --rebase flag each time you pull 🧐
Tip #2: Don't forget that you can do all sorts of editing to your commits, before pushing them by using interactive rebase.
Recover from git reset --hard
git reflog show4b6cf8e (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to origin/master295f07d HEAD@{1}: pull: Merge made by the 'recursive' strategy.7c49ec7 HEAD@{2}: commit: restore dependencies to the User modelfa57f59 HEAD@{3}: commit: restore dependencies to the Profile model3431936 HEAD@{4}: commit (amend): restore admin033f5c0 HEAD@{5}: commit: restore adminecd2c1d HEAD@{6}: commit: re-enable settings app# the commit the HEAD to be pointed to is 7c49ec7 (restore dependencies to the User model)git reset HEAD@{2}
Count all tracked files in a git respository
git ls-files notes| wc -l
git ls-files
: prints out a list of all the tracked files in the repository, one per line.|
: operator funnels the output from the preceding command into the command following the pipe.wc -l
: calls the word count (wc) program. Passing the-l
flag asks it to return the total number of lines.
Discard user email "xxxx" related commits after the specific commit
Create a backup of the repository:
git clone --mirror . <backup-folder>
git filter-branch --commit-filter 'if [ "$GIT_AUTHOR_EMAIL" = "REPLACE_EMAIL" ]; then skip_commit "$@"; else git commit-tree "$@"; fi' [REPLACE_COMMIT_ID]..HEAD
Checkout files/folders from a specific branch
rm -rf src .package.jsongit checkout main .src package.json
Branches
List all local branches
git branch
List all merged branches
git branch --merged
Same as above but exclude master and develop branches
git branch --merged | egrep -v "(^\*|master|develop)"
Deleting branches
git branch -d branch_name
Deleting remote branches
git push <remote_name> -d branch_name
Delete all branches but keeping others like “develop” and “master”
git branch | grep -v "develop" | grep -v "master" | xargs git branch -D
- grep: global regular expression - print lines matching a pattern
- v: invert-match, select non-matching lines
- xargs: read lines of text from the standard input or from the output of another command command and
turn them into commands and execute them
, which is used for building execution pipelines using the standard data streams
Delete remote branches
git push origin --delete branch1 branch2
Stashes
Save changes to a stash
git push origin --delete branch1 branch2
List all stashes.
git stash list
Annotate stashes with a description
git stash -u save 'message'
Re-apply the most recently created stash and delete it from stash list
git stash pop
Apply the specified stash by passing its identifier
git stash apply stash@{2}
Partial stash
git stash -p
Creating a branch from your stash
git stash branch add-stylesheet stash@{1}
Worktree
Git worktree is a powerful feature that allows you to work on multiple branches simultaneously without switching between them.
git worktree add ../feature-branch feature-branch
- Create a new directory named "feature-branch" in the parent directory of your current repository
- Check out the "feature-branch" in that new directory
Using git worktree allows you to:
- Work on multiple branches simultaneously without stashing or committing incomplete work
- Easily switch context between different tasks or features
- Perform operations like building or testing on one branch while actively developing on another
Tags
# list all git tagsgit tag -lgit tag --list# create a git taggit tag v1.9.2# remove a git taggit tag -d v1.9.2git tag --delete v1.9.2# push all tags to remotegit push --follow-tags origin master
Alias
A list of git aliases to include in your `~/.gitconfig` file:
[alias]s=statusbr = branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdateundo=reset HEAD~1 --mixedcm = commit --all -mcma = commit -aco = checkoutcob = checkout -bdiscard = reset --hard HEADdel= branch -Dst = stash -upop = stash popstat = log --shortstatd = diff --color-wordswho = shortlog -s --lg = log --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relativelpo = log --pretty=oneline --abbrev-commit --graph --decorate --allscrub = !git reset --hard && git clean -fdrv = remote -vsts = statusblg = log --graph --decorate --all --abbrev-commit --pretty=onelineslog = log --graph --simplify-by-decoration --all --abbrev-commit --pretty=onelinebusythisweek = shortlog --since=one.week.ago -n #https://git.wiki.kernel.org/index.php?title=Aliasesaliases = !git config --get-regexp 'alias.\*' | colrm 1 6 | sed 's/[ ]/ = /'whitespaceviolations = "!git diff --check $(git empty-tree-sha1)"app = commit -a --amend --no-editcia = commit -a --amendup = remote update --prunepublish = !git checkout -b $1 && git push -u originprb=pull --rebasesm=!git st && git co master && git prb