Git tracks snapshots of your project; GitHub hosts a copy so others can review it. Daily work is status, add, commit, switch, pull and push. To undo: restore discards file edits, reset moves your branch back, revert adds an opposite commit, and reflog finds lost commits. Merge keeps history; rebase rewrites it.
On this page
The Git commands you will use every dayUndo recipes: restore, reset, revert and reflogBranching, merge vs rebase and resolving conflicts.gitignore: what must never be committedYour first pull request, step by stepFrequently asked questionsThis page is for students and freshers who have used git add and git commit but freeze when something goes wrong: a wrong commit, a conflict, a deleted branch. Every transcript below was run by us in a throwaway repository on 1 December 2026 with git 2.54.0 (Apple Git-157). Commit hashes will differ on your machine; the shape of the output is the same.
Git is the tool on your laptop. GitHub is a website that stores a copy of a Git repository and adds pull requests, issues and review. They are separate: GitLab and Bitbucket host Git repositories too. For where Git sits in a working stack, read the tech stack explained.
- 1Editchange files in your working folder
- 2Stagegit add picks what goes in the next snapshot
- 3Commitgit commit saves the snapshot locally
- 4Pushgit push sends commits to GitHub
The Git commands you will use every day
The commands below cover most daily work. Each one is in the official git documentation. Run git status more often than feels necessary: it tells you what Git thinks is happening and usually suggests the next command.
| Command | What it does | Use it when |
|---|---|---|
git clone <url> | Copies a remote repository to your machine | Starting on an existing project |
git status | Shows changed, staged and untracked files | Before and after every other command |
git add <file> or git add -p | Stages a file, or chosen pieces of a file | Choosing what goes in the next commit |
git commit -m "message" | Saves the staged snapshot with a message | A small unit of work is finished |
git diff and git diff --staged | Shows unstaged changes, then staged changes | Reviewing before you commit |
git log --oneline --graph | Prints compact history with branch shape | Finding a commit or understanding a merge |
git switch <branch> and git switch -c <new> | Moves to a branch, or creates one and moves | Starting a feature or a fix |
git pull | Fetches remote commits and merges them in | Updating your branch from GitHub |
git fetch | Downloads remote commits without changing your files | Looking before you merge |
git push -u origin <branch> | Sends the branch to GitHub and remembers it | First push of a new branch |
git stash and git stash pop | Shelves uncommitted edits, then brings them back | You must switch branches mid-change |
git commit --amend | Rewrites your last commit message or contents | A typo in a commit you have not pushed |
git branch -d <branch> | Deletes a merged branch | Cleaning up after a merge |
A first session, exactly as git printed it. The last two commands show the safest undo of all: throw away an unstaged edit.
$ git init -b main
Initialized empty Git repository in .../g/.git/
$ git status --short
?? app.py
$ git add app.py
$ git commit -m "Add app.py"
[main (root-commit) 8968478] Add app.py
1 file changed, 1 insertion(+)
create mode 100644 app.py
$ echo "print('hello world')" > app.py
$ git diff
-print('hello')
+print('hello world')
$ git restore app.py
$ git status --short
$Undo recipes: restore, reset, revert and reflog
The right undo depends on two questions: did you commit, and did you push? If you pushed and others may have pulled, prefer revert, which adds a new commit and rewrites nothing.
| Situation | Command | Rewrites history? |
|---|---|---|
| Edited a file, want the last committed version back | git restore <file> | No, but the edit is gone for good |
Ran git add on the wrong file | git restore --staged <file> | No; your edit stays in the file |
| Last commit has a wrong message | git commit --amend -m "new" | Yes, only safe before pushing |
| Last commit was too early, keep the changes staged | git reset --soft HEAD~1 | Yes, local only |
| Throw away the last commit and its changes | git reset --hard HEAD~1 | Yes, and uncommitted work is lost |
| A commit is already pushed and shared | git revert <hash> | No: adds an undo commit |
| You reset too far or deleted a branch | git reflog, then git reset --hard <hash> | Restores the lost state |
git restore --staged and git commit --amend were also run in our temp repository and behaved as the table says. Next, the one that saves careers: you run git reset --hard HEAD~2, realise you needed those commits, and find them again with the reflog, a private list of where HEAD has been.
$ git reset --hard HEAD~2
HEAD is now at b301d38 Add config
$ git reflog -4
b301d38 HEAD@{0}: reset: moving to HEAD~2
aaeb682 HEAD@{1}: commit: Add b
621eef3 HEAD@{2}: commit: Add a
b301d38 HEAD@{3}: commit (initial): Add config
$ git reset --hard HEAD@{2}
HEAD is now at 621eef3 Add a
$ git log --oneline
621eef3 Add a
b301d38 Add configCommits are not erased by reset, only detached from the branch. The reflog is local to your machine and its entries expire after a while, so recover soon, not next month. Here is revert on a commit that is already shared:
$ git log --oneline
c934e26 Add y again
7e92497 Add x
8968478 Add app.py
$ git revert --no-edit HEAD
[main 28102f5] Revert "Add y again"
1 file changed, 1 deletion(-)
$ git log --oneline
28102f5 Revert "Add y again"
c934e26 Add y again
7e92497 Add x
8968478 Add app.pyOfficial references: git restore, git reset, git revert, git reflog.
Branching, merge vs rebase and resolving conflicts
A branch is a movable label on a commit. Create one for every change, however small, so main always works. Merge and rebase both combine two branches. They differ in what the history looks like afterwards.
| `git merge` | `git rebase` | |
|---|---|---|
| What it does | Adds a merge commit that joins both histories | Replays your commits on top of the other branch |
| History afterwards | Branches and merges stay visible | One straight line |
| Commit hashes | Existing commits unchanged | Your commits get new hashes |
| Conflicts | Resolve once, in the merge commit | May resolve once per replayed commit |
| Safe on shared branches? | Yes | No: never rebase commits others have pulled |
| Typical use | Bringing a finished feature into main | Updating your own feature branch with the latest main |
A conflict appears when both branches changed the same lines. Git stops, marks the file and waits. The text between <<<<<<< and ======= is your side; the text down to >>>>>>> is theirs. You pick the final text, delete the marker lines, add the file and commit.
$ git switch -c feature/greeting
$ # edit config.txt on this branch, commit
$ git switch main
$ # edit the same line of config.txt on main, commit
$ git merge feature/greeting
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Automatic merge failed; fix conflicts and then commit the result.
$ cat config.txt
<<<<<<< HEAD
title = "Main App"
=======
title = "Hello App"
>>>>>>> feature/greeting
version = 1
$ # edit the file to the final text you want, delete the three marker lines
$ git add config.txt
$ git commit --no-edit
[main b689860] Merge branch 'feature/greeting'Rebase on the same repository. The feature branch commit Add f is replayed after Add m, giving a straight line instead of a merge commit:
$ git switch feature/b1
$ git rebase main
Successfully rebased and updated refs/heads/feature/b1.
$ git log --oneline --graph --all
* 2c299bf Add f
* e0d68e8 Add m
* b689860 Merge branch 'feature/greeting'
|\
| * fce0475 Change title on feature
* | 3b71a35 Change title on main
|/
* 621eef3 Add a
* b301d38 Add configA workable team habit: rebase your private branch onto main to stay current, then merge it through a pull request. If a rebase goes wrong, git rebase --abort returns you to where you began. After you rebase a branch you already pushed, pushing needs git push --force-with-lease, which refuses if someone else pushed in the meantime. Details in the Pro Git chapter on rebasing and GitHub's about rebase.
.gitignore: what must never be committed
A .gitignore file lists patterns Git should not track. Commit it with the project. Typical starter for a Python or Node project:
# .gitignore
.env
*.log
__pycache__/
node_modules/
.venv/
dist/- Never commit
.envfiles, API keys, passwords or database dumps. If a key reaches GitHub, deleting the file is not enough: the key stays in history, so rotate it at the provider first. - Ignoring a file does not untrack one already committed. Run
git rm --cached <file>, then commit. git check-ignore -v <file>shows which rule is ignoring a file; we ran it and it printed the.gitignoreline number and pattern.- Generated folders (
node_modules/,dist/,__pycache__/) are rebuilt from your lockfile or source, so they do not belong in history.
Rules and pattern syntax: gitignore documentation.
Your first pull request, step by step
A pull request (PR) asks the project to merge your branch. We did not open a real PR for this page; the steps follow GitHub's creating a pull request page, read 1 December 2026.
- Fork the repository on GitHub if you do not have write access, then
git cloneyour fork. - Create a branch:
git switch -c fix-typo-readme. - Make one small change and check it with
git diff. - Stage and commit:
git add README.mdthengit commit -m "Fix typo in README". - Push the branch:
git push -u origin fix-typo-readme. - On GitHub, open the repository. A banner offers Compare & pull request. Pick the base branch you want to merge into.
- Write a title and a description that says what changed and why. Click Create Pull Request, or use the drop-down for a draft if it is not ready.
- Answer review comments by pushing more commits to the same branch; the PR updates itself. The GitHub CLI route is
gh pr create.
Keep your first PRs small: one change, one purpose. To build a profile that shows this work, see best GitHub profiles. For the build step that comes after a merge, read CI/CD pipeline explained, and for the career path that uses Git daily, the DevOps engineer roadmap and full stack developer roadmap.
Frequently asked questions
What is the difference between Git and GitHub?
Git is free version-control software that runs on your computer and records the history of your files. GitHub is a website that hosts Git repositories online and adds pull requests, issues and code review. You can use Git without GitHub, and other sites such as GitLab host Git repositories too.
What is the difference between git reset and git revert?
git reset moves your branch pointer back, so the later commits disappear from the branch history. It is for local, unpushed work. git revert adds a new commit that cancels an earlier one and leaves history intact, which is why it is the safe choice for commits you have already pushed.
How do I undo my last commit in Git?
To keep the changes, run git reset --soft HEAD~1; your files stay staged. To discard them, run git reset --hard HEAD~1. If the commit is already pushed and shared, use git revert HEAD instead. If you reset by mistake, git reflog shows the old commit hash.
Should I use git merge or git rebase?
Use merge to combine finished work into a shared branch; it never rewrites history. Use rebase to tidy or update your own private branch before opening a pull request. The rule: never rebase commits that other people have already pulled, because their copies will no longer match.
How do I recover a deleted branch or lost commit in Git?
Run git reflog and find the commit hash where the branch last was. Then git switch -c recovered <hash> creates a branch there, or git reset --hard <hash> moves the current one. This works only on your own machine, and old reflog entries are eventually cleaned up.
How do I resolve a merge conflict?
Open the conflicted file and look for the lines starting <<<<<<<, ======= and >>>>>>>. Keep the text you want, delete the marker lines, run git add <file>, then git commit. Use git merge --abort to cancel the whole merge and return to the state before it.
Learn this with mentors
Programmes for builders and engineers with admissions open: live classes, mentors and project work.


