`git stash` temporarily puts away uncommitted changes, and `git stash pop` restores them. `git commit --amend` rewrites the last commit — handy for fixing a message or adding a forgotten file.
- Quickly hide uncommitted changes and return to them later (git stash / stash pop).
- Fix the last commit without a new one: git commit --amend --no-edit.
git stash
# ...switched to another branch, did some work...
git switch feature
git stash popOutput:
Saved working directory and index state WIP on feature: a1b2c3d
Dropped refs/stash@{0} (b0f1a2b3...)git stash removed the changes, git stash pop restored them and removed the entry from the stack.
git add styles.css
git commit --amend --no-editOutput:
[feature 1c2d3e4] Add the home page
2 files changed, 15 insertions(+)--amend replaces the last commit, and --no-edit keeps its message unchanged.
Key rules:
- ✓git stash — hide uncommitted changes; git stash pop — restore them.
- ✓git commit --amend rewrites the last commit (safe while it is not pushed).
Common mistakes:
- !Do not amend already-pushed commits — it rewrites history and breaks your colleagues' clones.
`git fetch` brings new commits from the server but does not touch the working tree, while `git pull --rebase` replays your local commits on top. If two branches changed the same lines — a merge conflict occurs: Git marks the files and waits for a manual resolution.
- Bring others' commits separately from yours (git fetch) or sync at once (pull --rebase).
- Resolve a conflict: edit the files, git add, and finish the operation.
git fetch originOutput:
From https://github.com/user/project
a1b2c3d..d4e5f6a main -> origin/maingit fetch downloads new commits into origin/main but does NOT apply them to your local branch.
git merge feature
# git marked a conflict in index.html (<<<<<<< HEAD ... >>>>>>> feature)
# edited the file manually and then:
git add index.html
git commit -m "Merge feature: resolve conflict"Output:
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.Edits to conflicted files are staged with git add and finalized with a merge commit.
Key rules:
- ✓git fetch — bring commits without applying; pull --rebase — apply and replay your own.
- ✓Conflict: files are marked, fix manually → git add → git commit.
Common mistakes:
- !With `git pull` (no --rebase) extra merge commits are possible — prefer pull --rebase.
A hallmark of team work is the feature branch. `git push -u origin feature` publishes the branch on the server and remembers where to push next. Then a pull request (merge request) is created in the UI (GitHub/GitLab), code review happens on it, and the branch is merged into main.
- Publish a local branch and set up the upstream (git push -u origin <branch>).
- Understand the PR/MR process: feature branch → review → merge into main.
git switch -c feature-dark-theme
git push -u origin feature-dark-themeOutput:
* [new branch] feature-dark-theme -> feature-dark-theme
branch 'feature-dark-theme' set up to track 'origin/feature-dark-theme'.The -u flag sets the upstream: afterwards a plain `git push` is enough, without a remote name and branch.
Key rules:
- ✓git push -u links the local branch with the remote one (upstream).
- ✓PR/MR — a request to merge a feature branch into main after code review.
Common mistakes:
- !Without -u, git push requires explicitly spelling out the remote and the branch.
- !Before a PR it is better to sync the branch with main: git pull --rebase origin main.
`git tag` pins a named point in history — often a release version (v1.0.0). An annotated tag `-a` stores a message and the author. Conventional Commits is the accepted message format: `feat:` — a new feature, `fix:` — a bug fix, `BREAKING CHANGE` — breaking changes.
- Mark release versions (git tag -a v1.0.0 -m "...").
- Write clear, machine-readable commit messages (Conventional Commits).
git tag -a v1.0.0 -m "Release 1.0.0"
git push origin --tagsOutput:
* [new tag] v1.0.0 -> v1.0.0An annotated tag carries a message and the author; push --tags sends tags to the server.
git commit -m "feat: add a dark theme"Output:
[feature-dk a1e2f3a] feat: add a dark theme
1 file changed, 20 insertions(+)feat: — a new feature; fix: — a fix; BREAKING CHANGE — incompatible changes.
Key rules:
- ✓git tag -a creates an annotated version tag; git push origin --tags publishes the tags.
- ✓Conventional Commits: feat: / fix: / chore: / BREAKING CHANGE in the message.
Common mistakes:
- !Tags are created on a specific commit — create them when the branch is ready.