Git commands
This is the article for some important and frequent Git operations.
1. Git Branch
- Create a brand-new branch
feature
:
git checkout -b feature
- If already have a branch
feature
, switch to it:
git checkout feature
# or
git switch feature
- If you just cloned a remote repo but only downloaded the
main
branch, and wanted to create a local branch that track another remote branch:
git fetch origin --prune
git checkout -b feature origin/feature
- List remote branches:
git branch -r
- Safely deletes a branch (if fully merged into your current branch):
git branch -d feature
- Forcely deletes a branch (if not merged, use with care):
git branch -D feature
- Delte a remote branch
git push origin --delete feature
2. Git Reset
2.1 If you made a commit but now regret that commit, you can use:
- Standard Reset
- last commit disappears
- you still keep you latest file changes on your disk
git reset HEAD~1
- Hard Reset
- last commit disappears
- file back to how it was before the commit
git reset --hard HEAD~1
You can change the number to 2 or 3 to go back further.
2.2 If you want to retrieve remote updates and overwrite local updates
git checkout <branch_name>
git fetch origin
# reset your local branch to the remote tracking branch (check remote branch using git branch -vv)
git reset --hard <remote branch name>
# (optional) also remove any untracked files/dirs you created locally
git clean -fd
3. Git Merge
git merge X
means: "Combine the changes from branch X into the branch I’m on." It finds a common ancestor of the two branches, compares both branches to that base, and produces a new snapshot.
4. Git Push
Imagine you are now in every branch and want to upload your local changes into the corresponding remote branch. First, confirm where you are:
git branch --show-current
First time push the branch? Do:
git push -u origin feature
Already pushed before? Just do:
git push
Git usually will upload your changes to the corresponding remote branch. To check which remote branch this local branch is mapping into, use:
git branch -vv
5. Git Add
git add -A
stages all changes in the repo:
- New files
- Modified files
- Deleted files
git add .
stages:
- New + modified files
- Doesn’t stage deletions outside that directory (gotcha when run in subfolders).
git add -u
stages:
- modified + deleted files
- don't stage new files
6. Git Rebase
rebase = replace my commits on top of a new base to make history linear.
Below are typical workflows:
6.1 Update your feature with the latest main
Imagine you are in the feature
branch, and the remote main
branch has updates. You need to retrieve the remote updates in main
branch into local and place your newest feature
changes as a branch of the newest main
commit. So you can keep you feature
branch update to date.
git checkout feature
git fetch origin
git rebase origin/main
git push --force-with-lease # update the feature branch
Simple exmaple: imagine multiple people work on a file in the main branch:
(main branch)
a = 1
You make a local new branch test to the project, and you add a line into that file:
a = 1
b = 2 # from test branch update
However, in the meantime, the main branch is updated by others, and now its content becomes:
a = 1
c = 3 # from main branch update
After you fetch and rebase, the file in your test branch will become:
a = 1
c = 3 # from main branch update
b = 2 # from test branch update
Then you git push
to upload your newest feature branch to the remote.
If you even need to merge/PR your feature into main branch, use:
git push --force-with-lease -u origin my-feature
6.2 Clean up your commits (interactive)
If you have too much trivial commits and you want to combine multiple into one, you can use (change the number as need):
git rebase -i HEAD~4
7. Typical Workflow of Making a PR
- Start a new branch
feature
:
git checkout main
git checkout -b feature
- Make changes and commit (repeat edit → add → commit as needed.)
# you made some changes
git add -A
git commit -m "..."
- Keep your branch up to date with main
Fetch remote updates in the main
branch and rebase your changes:
git fetch origin
git rebase origin/main
- Push and set upstream
If this is the first time making a PR:
git push -u origin feature
If you already set the upstream:
git push --force-with-lease
-
Open the PR (on GitHub)
- Base:
main
- Compare:
feature
- Use a clear title, bullet the changes, link issues.
If the PR drifts behind
main
, update:bash git fetch origin git rebase origin/main git push --force-with-lease
- Base:
-
Merge the PR
Typically just choose Squash merge.
- Sync and clean up
# Update local main so it's got the merged PR
git switch main
git pull --ff-only
# Delete the remote branch
git branch -d feature
# Delete the local branch (safe: only if merged)
git push origin --delete feature
- Cleans up stale remote-tracking branches in your local repo
When a branch is deleted on the remote (e.g., feature-x
), your local ref feature-x
can linger. git fetch --prune
removes those dangling local refs so git branch -r
only shows branches that still exist on the server.
git fetch --prune
8. Git Pull & Fetch To Get Remote Updates
Imagine you are working on the main
branch on your own repo from multiple device. You plan to push your local changes but failed because the remote has some updates from another device. So you try git pull
but has conflict. Here is to fix it:
If you didn't commit your local edits
git stash push -m wip # stash your edits
git pull --ff-only # update main cleanly
git stash pop # bring edits back; fix any conflicts
If you have commited your local edits (oops)
If you can't push into the main
branch directly, turn them into a feature branch, then rebase and push:
git checkout -b feature # branch off where you are
git checkout main
git reset --hard origin/main # reset main to remote clean state
git checkout feature
git rebase origin/main # integrate remote changes
git push -u origin feature
If you want to keep local commits on main and integrate remote
If you can push into main
branch directly, use:
git fetch origin
git rebase origin/main # while on main; fix conflicts → continue
git push
Difference between Fetch and Pull
git fetch = download updates only (no changes to your files/branch).
git pull = fetch + integrate into your current branch
9. Core Habits
One-time config (highly recommended)
# Keep pulls linear on non-main branches
git config --global pull.rebase true
# Never create merge commits when updating main
git config --global pull.ff only
# Auto-clean stale remote branches
git config --global fetch.prune true
If working on non-main
branch:
git checkout main
git pull --ff-only
git checkout feature
If working on main
branch:
git fetch origin
git rebase origin/main