version control system (VCS) designed to track changes in files
-
allows multiple developers to work on the same project without interfering in each others changes
-
branch: allow you to work on different versions of your project at the same time without mixing up new features or bug fixes until they’re ready
-
commit: snapshots of your project’s history
- each commit records changes to one or more files and includes a short message describing what changed
-
push: sends your local commits to github
-
pull: fetches and merges changes from github to your local machine
-
fetch: retrieves changes from github but doesn’t merge them automatically, giving you a chance to review before merging
-
HEAD: current branch
-
SHA-1 hash: unique identifier for a commit
-
upstream branch: branch that the current branch is tracking
-
downstream branch: branch that is tracking the current branch
-
index: staging area
-
work tree/working tree: set of files that represent your project
- set up with
git initorgit clone
- set up with
-
main working tree: when you run
git init, git creates the main working tree
git cat-file -p <hash>: SHA-1 hash can be used to see the contents of a commit- inspect the contents of a git object (like a commit, tree, blob or tag) in a human-readable format
git show abc123def456: show git diff using commit hashgit log --grep="fix container image push": search commit messagesgit log -S <pattern>: search code code/content changes of each commitgit log -L :my_function:app.py: show every commit that modifiedmy_function()inapp.py, with diffsgit log origin/myfeature: log the commits of remote repogit log --oneline --graph: show history of commits in a graphgit blame <file>: show commit hash, author, timestamp for each line of codegit branch --set-upstream-to=origin/master master: set the default upstream branch for the master branch
git switch <branch>: switch to a branch- create a new branch and switch to it:
git switch -c <branch> - create a new branch from a specific commit:
git switch -c <branch> <commit_hash>
- create a new branch and switch to it:
git remote -v: show the remote repositoriesgit remote add <name> <url>: add a remote repositorygit remote add origin ~/Documents/remote-git: the remote can be local or on the internetgit reset --soft HEAD~1: undo the last commit while keeping the changes
git stash list: show the list of stashesgit stash -m 'message': create a stash with a messagegit stash pop --index <stash index>: apply the last stashgit cherry-pick <commit>: apply a commit to the current branchgit reflog: show the history of the HEADgit config --global alias.<alias> <command>: create an alias for a commandgit config --list: show the git configurationgit log --grep <pattern> -p: show the commits that match the pattern-pshows the diff
git bisect start: start the bisect processgit bisect bad: mark the current commit as badgit bisect good <commit>: mark a commit as goodgit bisect reset: stop the bisect processgit bisect run <command> --run: Automatically find the commit that introduced a bug--runis the command to run
git worktree add ../<new-dir/branch: create a new worktreegit rebase <branch2>: Rebase from branch1 onto branch2 (run this command on branch1)
Warning
You should never rebase a public branch (like main) onto anything else. Other developers have it checked out, and if you change its history, you'll cause a lot of problems for them. However, with your own branch, you can rebase onto other branches (including a public branch like main) as much as you want.
- branches:
main: production-ready codedevelop: integration branch for featuresfeature/*: new features (branched fromdevelop)release/*: prep for release (branched fromdevelop)hotfix/*: urgent production fixes (branched frommain)
worflow:
- create
feature/login-system=> merge intodevelop - create
release/v1.0=> test, bugfix => merge intomainanddevelop - create
hotfix/urgent-bug=> merge intomainanddevelop
- pros: separates environments clearly
- cons: overhead for small teams, many merges
TODO
TODO
TODO
specifies which files and directories git should ignore
- files that should typically be ignored:
- build artifacts and compiled code
- dependencies and package directories
- environment and config files with secrets
- system files (.DS_Store, Thumbs.db)
- ide files and directories
- log files
- temporary files
# Ignore single file
secret.txt
# Ignore directory
node_modules/
dist/
# Ignore files by extension
*.log
*.tmp
# Ignore files in any directory
**/*.pyc
# Negate ignore (don't ignore)
!important.log
# Ignore files in root only
/config.json
# Ignore files in specific directory
logs/*.log- merging branch with upstream problems
- i dont understand why it is necessary to create 2 merge commits to fix the different past of the branch when rebasing
- should i rebase private branch into public branch before pushing?