Git can fire off custom scripts when certain important actions occur.
- pre-commit
- prepare-commit-msg
- commit-msg
- post-commit
We can bypass pre-commit and commit-msg hooks using the following command:
git commit -m "tidy" --no-verify
- Create and switch to a new branch
git switch -c path_config
# Switched to a new branch 'path_config'
- List all local branches
git branch --all
- List all the remote brances
git branch -r
- Reset all changes after last commit
Undo chnages to tracked file:
git reset HEAD --hard
Remove untracked files:
git clean -f
Remove untracked files and directories:
git clean -fd
Remove GIT History (Caution it delete all the commit history)
git checkout --orphan last
git add -A
git commit -am "fresh init"
git branch -D main
git branch -m main
git push -f origin main
- Bare repository
git clone --mirror [email protected]:lifeparticle/binarytree.git
- Reduce Git repository size
git clone --mirror [email protected]:lifeparticle/binarytree.git
bfg --strip-blobs-bigger-than 1M binarytree.git
cd binarytree.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive
- How do I reset master to origin/master?
If -B is given, <new_branch> is created if it doesn’t exist; otherwise, it is reset
git checkout -B master origin/master
- Rollback a Git merge
With git reflog check which commit is one prior the merge
git reflog
git reset --hard commit_sha
- Delete a local branch
Switch to another branch and delete test_dev_branch
git checkout master
git branch -d <branch>
If the branch test_dev_branch is not fully merged you will get an error but you can force delete it using -D instead of -d
git branch -D <branch>
Delete test_dev_branch from remote
git push origin --delete <branch>
- Pull from master into the development branch
git checkout test_dev_branch # gets you on test_dev_branch
git fetch origin # gets you up to date with origin
git merge origin/master
- Switch to another branch
git checkout <branch>
- Create a new branch
git branch <branch>
- Git squash
Last three commits
git rebase -i HEAD~3
change the file
pick 12345 Commit 1
squash 54321 Commit 2
squash 98765 Commit 3
save and quit
fix the commit message
push
git push origin DEV-244 -f
- Empty directorie
https://git.wiki.kernel.org/index.php/GitFaq#Can_I_add_empty_directories.3F
- Remove .gitignore files from github
git rm -rf --cached .
git commit -m "rm files"
git push
git rm -r --cached .