Version control is a system that tracks changes in files over time. Git is used to:
- Track code changes
- Collaborate with others
- Roll back to previous versions
- Manage different versions using branches
Git
- Tool/software
- Runs locally
- Tracks versions
- Command-line based
GitHub
- Online platform
- Runs on cloud
- Hosts Git repositories
- Web UI + collaboration
A repository stores the project history, objects and branch/tag references. Git is distributed, so each clone can have its own repository. A normal repo has a working folder and usually a .git folder. A bare repo has no working folder.
Use git init to start one and git clone to copy an existing one.
- A commit is a snapshot of the project at a point in time, along with a message describing what changed.
- A branch is a separate line of development used to work on features or fixes independently from the main code.
HEAD normally points to the branch currently checked out. In detached HEAD state, it points directly to a commit.
git fetch downloads changes and updates remote-tracking branches. It does not add those changes to the current branch.
git pull fetches and then adds the changes using merge, rebase or fast-forward-only, based on the options and configuration.
git pull --ff-only
git pull --rebase
git pull --no-rebaseA merge conflict happens when Git cannot combine changes automatically. It can happen with edits, renames or a file deleted on one branch and changed on another.
Check the files, resolve the conflicts, stage them, run tests and continue. To cancel the merge, use abort.
git status
git add FILE
git merge --continue
# To cancel the merge instead:
git merge --abort.gitignore tells Git which untracked files to ignore, like .env or build files. It does not stop tracking a file already committed.
To keep .env locally but remove it from the next commit:
git rm --cached .env
# Add .env to .gitignore, then commit both changes.If it contains a real secret, rotate the secret too. Ignoring the file does not remove its old copies.
-
Clone → local copy of a repo.
-
Fork → copy of someone else's repo on your GitHub account.
Check git status and save important work before undo commands. Practise destructive commands in a test repo. Use the actual branch name; main below is only an example.
# Start a repo
git init
# Stage one file
git add file.txt
# Stage changes below the current folder
git add .
# Stage changes across the whole repo
git add -A
# Commit and inspect
git commit -m "message"
git status
git log
# Create and switch branch
git checkout -b feature
# Delete a local branch
git branch -d feature
# Push and pull, using an example main branch
git push origin main
git pull --ff-only origin main
# Undo the last unpublished commit, keeping changes staged
git reset --soft HEAD~1
# Undo it, keeping files but clearing staging
git reset --mixed HEAD~1
# Destructive: also resets working files; can lose local work
git reset --hard HEAD~1
# Restore an unstaged deletion
git restore -- FILE
# If deletion was staged, unstage first, then restore
git restore --staged -- FILE
git restore -- FILEHEAD~1 means the first parent of HEAD. For a bad commit already shared with the team, use git revert COMMIT instead of removing it from history.
Revoke or rotate the secret first. Then remove it from the code and stop tracking the secret file. Add the ignore rule and check where the secret was exposed.
For pushed history, coordinate with the team and follow GitHub's cleanup steps using git-filter-repo. A local reset does not clean remote copies, clones or forks. Deleting the file does not make the leaked secret safe.
A conflict may happen when the branches are combined. It is not guaranteed just because both people changed the same line. Identical changes can merge cleanly.
Resolve the conflict, stage the files, run tests and continue the merge or rebase. Use abort to cancel it.
Rebase is useful for cleaning up a feature branch before sharing it. It reapplies commits on a new base and usually gives them new commit IDs. Do not rebase shared commits without team agreement.
Merge keeps the existing commits. A fast-forward merge only moves the branch pointer.
After a rebase conflict, fix the files, stage them and run git rebase --continue. Use git rebase --abort to cancel.
Add the original repo as upstream, fetch its changes and merge its actual default branch. For example, if that branch is master:
git remote add upstream <original-repo-url>
git fetch upstream
git switch master
git merge upstream/masterSave local work first. Do not assume every repo uses main.
The working tree is where files are edited. The staging area, or index, holds the next snapshot to commit. A commit saves that snapshot with its details.
If a file is changed again after git add, that new change must be staged again to include it.
git diff shows unstaged changes. git diff --staged shows staged changes compared with HEAD. git diff HEAD compares tracked working files with HEAD. These commands do not show newly untracked files.
git status
git diff
git diff --stagedUse git restore --staged. It removes the change from staging but keeps the working file.
git restore --staged -- FILEBe careful: git restore FILE without --staged normally discards unstaged changes by restoring the file from the index.
Stash saves local work temporarily. By default, it saves tracked changes. Use -u for untracked files and -a for ignored files too.
apply restores the changes and keeps the stash. pop restores and removes it only if that succeeds. On a conflict, the stash stays. --index also tries to restore what was staged.
git stash push -u -m "WIP before hotfix"
git stash list
git stash applyA remote is a saved name for a repo URL. origin is normally created by clone. In a fork, upstream often means the original repo.
origin/main is a local record of the remote branch from the last fetch. It is not a live view. A branch's upstream tracking setting is separate from a remote named upstream.
Use push with -u. It sets the tracking branch for later push/pull commands. Check it with git branch -vv.
git push -u origin feature
git branch -vvUse restore for files or staging. Use reset to move a branch tip or reset staged entries. Use revert to make a new commit that reverses an old commit.
For a normal bad commit already shared with the team, revert keeps the history and is usually the safer choice.
git revert COMMITReflog records local branch and HEAD movements. Find the old commit, inspect it and make a recovery branch.
git reflog
git show COMMIT
git branch recovered COMMITReflog is local, not a backup. Entries expire and old objects can be removed, so recovery is not always possible.
Detached HEAD means HEAD points directly to a commit instead of a branch. New commits can be made, but no branch moves to keep them.
Before leaving, run git switch -c keep-work. If the commit was already left behind, find it in reflog and create a branch at that commit.
Cherry-pick applies one selected commit's change to the current branch, normally as a new commit. It is useful for taking one hotfix without merging the full feature branch.
git cherry-pick COMMIT
# After resolving and staging conflicts:
git cherry-pick --continue
# Or cancel:
git cherry-pick --abortUse amend to change the last commit's message or add staged changes. It replaces that commit with a new one. Use it for unpublished work; do not change shared history without agreement.
git commit --amend -m "Clearer commit message"
# After staging extra changes:
git commit --amend --no-editThe remote branch has commits missing from the local branch. Fetch, inspect the changes and merge or rebase using the team's workflow. Then test and push. Do not force-push just to remove the error.
--force can overwrite remote history. --force-with-lease checks that the remote branch still matches the expected commit before updating it.
It is safer, but it can still replace work. Background fetches can change the expected local tracking value. Use it only for an agreed history rewrite; giving the exact expected commit makes the check stronger.
A branch normally moves as new commits are made. A tag normally stays on one version. A lightweight tag is a reference. An annotated tag also stores a message, tagger and date, and can be signed.
git tag -a v1.0.0 -m "Release 1.0.0"
git push origin v1.0.0Use interactive rebase for unpublished commits. For the last three commits, run git rebase -i HEAD~3. Keep the first as pick and mark later commits as squash or fixup.
This changes commit IDs. Do not use it on shared history without agreement.
Use bisect with a known good and bad commit. Git checks commits in between. Run the test and mark each one good or bad until the first bad commit is found. The test must give a reliable result.
git bisect start
git bisect bad
git bisect good GOOD_COMMIT
# Test each commit, then run git bisect good or git bisect bad
git bisect resetStart from an updated base and create a feature branch. Make focused commits, check staged changes, run tests and push. Open a pull request for review and checks.
Bring in main/master changes using the team's merge or rebase workflow. Keep secrets out of commits and do not rewrite teammates' work. A pull request is a hosting-platform review step, not a Git commit type.
No. git branch -d deletes a local branch and normally checks that its work was merged. Remote deletion is a separate command.
git branch -d feature
# Delete the remote branch only after team agreement:
git push origin --delete featuregit fetch --prune removes stale local remote-tracking references. It does not delete ordinary local branches.
Git stores each commit as a snapshot of the whole project (unchanged files are just linked to the old version). Everything is kept in the hidden .git folder as objects: blob (file content), tree (folder), commit (snapshot + message + parent) and a SHA-1 hash ID for each. Branches and tags are just names pointing to commits.
The folder where Git keeps the whole repository: history, objects, branches, config, HEAD. If you delete .git, the project is no longer a Git repo (you lose all history). Never edit it by hand.
Short summary line in imperative style (for example "Add login validation"), optionally a blank line and a detail. One logical change per commit. Commit small and often; do not commit broken code, secrets or big binaries. Many teams use a prefix like feat: / fix:.
A request on GitHub/GitLab to merge your branch into another (usually main). Teammates review the code, comments are added, CI tests run, and after approval it is merged. It is a platform feature, not a Git command.
HTTPS: URL with your username and a personal access token (passwords are no longer accepted by GitHub). SSH: uses a key pair; you add the public key to GitHub and never type a password again. Both work; SSH is more convenient for daily use.
git config --global user.name "Aalia" and git config --global user.email "me@example.com". They appear in every commit. --global is for all repos, without it only for the current repo. Check with git config --list.
git log --oneline
git log --oneline --graph --all
git log -p FILE # changes made to a file
git log --author="Aalia"
git show COMMIT # one commit in detailgit rm file deletes the file and stages the deletion in one step. If you delete with the file manager, you still need git add -A (or git rm) to record the deletion. git rm --cached file stops tracking but keeps the file on disk.
Removes untracked files from the working folder. It is destructive, so first do a dry run: git clean -n, then git clean -fd (files and folders).
Shows who last changed each line of a file and in which commit: git blame FILE. Used to understand why a line was changed, not to blame people.
Fast-forward: the target branch has no new commits, so Git just moves the pointer (straight history). Merge commit: Git creates a new commit with two parents, preserving branch history (--no-ff forces this). Squash merge: all branch commits become one single commit on the target, giving a clean history but losing individual commits.
Git marks the file:
<<<<<<< HEAD
your version
=======
incoming version
>>>>>>> feature
Open the file, keep the correct code (or combine both), remove the markers, git add FILE, then git commit (or git merge --continue). Run the tests. Tools like VS Code show "Accept current / incoming / both".
Pull/rebase from main often, keep branches short-lived, make small PRs, and talk to teammates when working on the same file.
Git Flow: long-lived main + develop, plus feature, release and hotfix branches (more process, for scheduled releases). Trunk-based: everyone merges small changes to main frequently with feature flags and CI (faster). Many companies use a simple "feature branch + PR to main".
Teams protect main: no direct push, PR required, at least one approval, CI must pass. So never push straight to main unless the team allows it.
For normal commits both mean the parent of HEAD. HEAD~3 means three commits back (following first parents). HEAD^2 means the second parent of a merge commit.
Pushed commit: git revert COMMIT makes a new commit that undoes it (safe for shared branches). Merge commit: git revert -m 1 MERGE_COMMIT (-m 1 keeps the main branch side as the base).
If not pushed: create the right branch from here (git branch correct-branch), then move the wrong branch back (git reset --hard HEAD~1, check first that nothing else is lost). Or cherry-pick the commit onto the correct branch and reset the wrong one.
Committed work: yes, use git reflog, find the old commit and git branch recovered COMMIT. Uncommitted changes in the working folder are usually gone.
Merge keeps true history and adds a merge commit. Rebase replays your commits on top of the latest base to make a straight history (new commit IDs, never on shared branches).
Scripts in .git/hooks that run on events (pre-commit, pre-push, commit-msg). Used for lint, tests and message checks. They are local; teams share them with tools like husky or pre-commit.
CI (continuous integration): on every push/PR a server builds the code and runs tests automatically (GitHub Actions, GitLab CI, Jenkins). CD (continuous delivery/deployment): after tests pass, the app is released/deployed automatically or with one click.
git clone URL
git status
git add . && git commit -m "msg"
git switch -c feature
git push -u origin feature
git fetch && git pull --ff-only
git merge feature
git stash / git stash pop
git log --oneline --graph
git reflog