basic config to set in file .gitconfig
git config --global pull.rebase true
git config --global fetch.prune true
git config --global diff.colorMoved zebra1. basic commands
-
git clone https://…: clone repo (default branch)git clone -b user --single-branch https://github.com/[...].git: clone specific branch
-
git status -
git add .git add a_specific_folder/*
-
git ls-files --deleted | xargs git add: git add only the deleted files -
git commit -m ‘my message to commit’ -
git commit -am ‘my message to commit': add and commit -
git push origin master -
git push -f origin master: force a push (pretty dangerous cause it overwrite the whole commit history) -
git pull origin master -
git branch: listgit branch -a: list local and remote branches
-
git checkout mybranch: change branch -
git branch -m branch_old branch_new: rename branch -
git reset: remove all git add files (staged) -
git reset --hard HEAD: erase all modif (does not erase new files) and go back to last pull / commit -
git checkout -- <file>: reset only one file -
git restore --staged <file>: remove file from git add -
git log- `git log —stat
git log --oneline -5
-
git diff --statto get the insertions/deletions statsgit diffdetailed differencesgit diff <particular_file>detailed differencesgit diff --cachedfor staged filesgit diff --numstatfor the number of diff
-
git remote -v: check the current origin urlgit remote set-url origin [<https://github.com>](<https://github.com/username/repo-name.git>)…: change the origin url
-
git fetch —allupdate all branches local and remote (esp. if there are remote branches that are not listed in git branch -a) // but does not update the branches, need to pull also →git pull --all
git commit -m 'chore: update notes'
git commit -m 'chore: add notes'
git commit -m 'feat: custom styling'
git commit -m 'fix: rendering of X feat'- count number of untracked files
git status --porcelain | grep '^??' | wc -l
2. branch manipulation
- create new branch
devfrommaster
git checkout master
git checkout -b dev master # equivalent to: git branch dev && git checkout dev ?
git push origin dev- merge branch
devtomaster(i.e. updatemasterby integratingdevchanges)
note: you’re always calling merge when located on the branch you want the changes to be integrated in, in this casemaster.
git checkout master
git merge dev
git push origin master- update
userwithmaster
git checkout user
git merge master
git push origin user- update branch
userwith latest commit (resetandpull)
git status
git branch
git reset --hard
git pull origin user- delete branch
# delete branch locally
git branch -d localBranchName
# delete branch remotely
git push origin --delete remoteBranchName3. repo manipulation
- update repo with changes in .gitignore file
git rm -r --cached .
git add .
git commit -m 'blabla'
git push origin master4. filter and pre-hook
pre-commit= script to run before commit
ls -la .git/hooks/
chmod +x .github/hooks/pre-commit
cp .github/hooks/pre-commit .git/hooks/pre-commit- scripts to run when
git addin called:- e.g.
git config filter.cleancomments.clean "/path/to/your/clean-script.sh"- with
.gitattributes:
- e.g.
# Apply comment filter to all markdown files
*.md filter=cleancomments
- scripts can be written in python, and can be tested:
python .github/hooks/pre-commit
provided the scripts are:
#!/usr/bin/env python3
...
if __name__ == '__main__':
sys.exit(main())x. troubleshootings
- how to delete latest pushed commit:
- note: it will also remove the commit from the history on github 👌
git log: copy the commit id- ⚠
git reset --hard <commit_id>orgit reset --hard HEAD~1→ careful, this definitely impacts your local files
- ⚠
git reset --soft HEAD~1→ this is better because this keeps files and removes last commitgit push --force(that is so dangerous tho)
- how to reverse a
git reset --hard HEAD^(cf. link)
x. good practices
- commit messages
<type>[optional scope]: <description>feat: new featurefix: bug fixdocs: documentation changesstyle: formatting, missing semicolons, etc (no code change)refactor: code restructuring, no bug fixes or featuresperf: performance improvementstest: adding/updating testschore: build process, config files, dependencies