- Git commands
- Working Directory: File you edit, can be unstaged or staged.
- Staging Area: Changes marked for the next commit, git add
- Local Repository (.git): the hidden .git database that stores all commits, branches, and history on your machine.
- Remote Repository (GitHub)
Working Directory
↓ git add
↑ git restore
Staging Area
↓ git commit
↑ git reset HEAD
Local Repository- Unstage == Keep file changes
- The local repository is the hidden .git database that stores all commits, branches, and history on your machine.
Q: How to “see” the local repository. See commits.
A: git log --oneline
Q: How to “see” the local repository. See branches (local repository)
A: git branch
- Save changes to the staging area for the next commit.
- Working Directory ==> State Area
- Unstage a file
git add xxx→ stage a file git restore --staged xxx → unstage a file
- stage changes <=> unstage changes
- show current repository state, staged vs unstaged changes.
- Red → unstaged
- Green → staged
- Save staged changes to the local repository.
- Undo commit and unstaged
git add → stage changes
git restore --staged → unstage changes
git commit → save staged changes
git reset --soft → undo commit, keep staged
git reset → undo commit, keep unstaged- Edit the latest commit message
git fetch→ download remote updates TOremote-tracking branches(==NO visible changes)git merge→ merge another branch into the current branch.git pull→ fetch + merge (automatic)- Fetch is like checking the menu.
- Merge is like ordering the food 🍔
origin/main==> local, read-only references of the remote main branch. (== remote-tracking branches)origin/feature-test==> local, read-only references of the remote feature-test branch. (==remote-tracking branches)
// I am main branch.
git fetch origin # update remote-tracking branches
git diff main origin/feature-test # preview changes before merge
git merge origin/feature-test # apply changes to main- upload local commits to the remote repository
- show changes in working directory vs staging area
# summary (Branch)
git branch: List all local branches.
git branch xxx: Create a new branch.
git checkout -b new-branch: Create a new branch and switch to it.- List all local branches
- Create a new branch (does not switch to it)
- Create a new branch and switch to it immediately
- temporarily save uncommitted changes to reapply later
git stash
git pull origin master
git stash apply- Copy a remote repository to a local directory
# Summary
git stash : temporarily save uncommitted changes to reapply later.
git stash apply:- git merge combines another branch into your current branch by creating a new merge commit that preserves both histories.
git merge develop
A — B — C ──┐
\ M (my-branch)
D — E- M = merge commit
- D and E keep their original hashes
- History is non-linear
- No history rewriting
- ✅ Creates 1 new merge commit
- git rebase rewrites commit history by replaying commits on top of another base commit, creating a linear history.
git rebase develop
A — B — C — D' — E' (my-branch)- D → D'
- E → E'
- No merge commit
- Commits are replayed on top of C
- Commit hashes change
- ✅ Creates new commits, but not a merge commit
- Normal rebase is for syncing code; interactive rebase is for cleaning history.
git rebase develop: syncing code
git rebase -i [branch]:interactive rebase is for cleaning history before pushing / opening PRFlow
# Update develop
1. git checkout develop
2. git pull origin develop
# Switch to your feature branch
3. git checkout my-branch
4. git rebase develop ## git merge vs git rebase
### !!! If conflicts happen → fix them, then:
5. git add .
6. git rebase --continue
### Then, I keep working on my-branch to complete my tasks.
7. git rebase -i develop ## interactive rebase to edit your own commits (squash, reorder, rename) on top of develop.
8. git push origin my-branch --force-with-lease## Before rebase
A — B — C (develop)
\
D (feature1)
## After rebase
A — B — C — D' (feature1)- interactive rebase is for cleaning history before pushing / opening PR
pick 0a2a79e test 2/11 ## Keep a first commit
squash 36d7e81 commit test 2 # squash
squash fb0cf39 commit test -3 # squash
OR
pick 0a2a79e test 2/11 ## Keep a first commit
s 36d7e81 commit test 2 # squash
s fb0cf39 commit test -3 # squash
// Escape wq!
// Add one clean commitTo get a linear history, teams usually require:
- rebase (not merge)
- squash feature commits
- fast-forward merges only
- Show commit history with graph
git log --oneline → compact commit history
git log --oneline --graph → commit history with graph- Merge Conflict occurs when changes made to the same part of the same file on two different branch.
- Resolve by checking the conflict markers (HEAD for current branch, incoming branch markers) and fixing manually.
- pointer to the current commit on the checked-out branch.
git checkout develop
git merge feature1
# A — B — C — D' (develop)