basic config to set in file .gitconfig

git config --global pull.rebase true
git config --global fetch.prune true
git config --global diff.colorMoved zebra

1. 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 : list

    • git 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 --stat to get the insertions/deletions stats

    • git diff detailed differences
    • git diff <particular_file> detailed differences
    • git diff --cached for staged files
    • git diff --numstat for the number of diff
  • git remote -v : check the current origin url

    • git remote set-url origin [<https://github.com>](<https://github.com/username/repo-name.git>)… : change the origin url
  • git fetch —all update 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 dev from master
git checkout master
git checkout -b dev master # equivalent to: git branch dev && git checkout dev ? 
git push origin dev
  • merge branch dev to master (i.e. update master by integrating dev changes)
    note: you’re always calling merge when located on the branch you want the changes to be integrated in, in this case master.
git checkout master
git merge dev 
git push origin master
  • update user with master
git checkout user
git merge master
git push origin user
  • update branch user with latest commit (reset and pull)
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 remoteBranchName

3. repo manipulation

  • update repo with changes in .gitignore file
git rm -r --cached .
git add .
git commit -m 'blabla'
git push origin master

4. 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 add in called:
    • e.g.
      • git config filter.cleancomments.clean "/path/to/your/clean-script.sh"
      • with .gitattributes:
# 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> or git 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 commit
    • git 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 feature
    • fix: bug fix
    • docs: documentation changes
    • style: formatting, missing semicolons, etc (no code change)
    • refactor: code restructuring, no bug fixes or features
    • perf: performance improvements
    • test: adding/updating tests
    • chore: build process, config files, dependencies

resources