Back to the blog

Git without fear: commit, branch, remote explained, then the commands that save you

Code6 min read · 27 September 2026

The GetPack team

You messed something up with Git. A commit on the wrong file, a file deleted by mistake, or the word “CONFLICT” showing up in your terminal with no idea what to do next. First thing to know: Git almost never actually deletes anything, even when it looks scary. Everything you’ve committed lives somewhere, recoverable. This article covers the three basic concepts, then the commands that get you out of the most common situations, all verified against Git’s official documentation.

Three words to understand Git

A commit is a snapshot of your project at a given moment: every file, exactly as it was when you saved. A branch is just a name pointing at a commit; when you add a new commit, that pointer moves forward automatically. A remote (usually named origin) is a copy of your repository hosted elsewhere, typically on GitHub: it’s what you sync with through git push and git pull.

Until you run git push, everything you do stays local, on your machine. That matters: most of the troubleshooting commands below only touch your local history, with zero risk for anyone else.

Undoing your last commit without losing your work

You committed too early, or with a messy message. Here’s the command:

git reset HEAD~1

HEAD~1 means “the commit right before the one you’re on.” By default, git reset runs in --mixed mode: it moves the branch pointer back, but leaves your working directory untouched. In practice, your commit disappears, but the content you had committed ends up back as unstaged changes, ready to be committed properly.

If you genuinely want to throw away the changes too (not just the commit), there’s git reset --hard HEAD~1. Be careful with --hard: it overwrites your working directory files to match the target commit, and tracked files that didn’t exist at that commit get removed. The official documentation is blunt about where the line is: never do this if you’ve already shared those commits with somebody else (in other words, if you’ve already pushed them to a remote other people use).

Local safety net: before every reset, Git automatically records where you were in a special reference called ORIG_HEAD, so you can back out if you ran the wrong command.

Recovering a file you broke or deleted by mistake

To restore a single file to its last-committed state, without touching anything else:

git restore path/to/file

By default, git restore restores the file from the index (whatever you already staged with git add), or from HEAD if you add --staged. You can also reach further back in history with the --source option:

git restore --source=HEAD~2 path/to/file

That brings the file back to how it looked two commits ago, without changing anything else in your project. It’s the safest tool when you want to undo changes to one specific file without touching your commit history.

Resolving a conflict without panicking

A conflict happens when Git merges two histories that changed the same lines differently (typically during a git merge or a git pull). Git opens the affected file and drops in markers:

<<<<<<< HEAD
# My Project
=======
# My Awesome Project
>>>>>>> feature-readme

Everything between <<<<<<< HEAD and ======= is your current version; everything between ======= and >>>>>>> feature-readme comes from the other branch. There’s no mystery to it: open the file, decide what to keep (one version, both, or a rewrite), then delete the three marker lines entirely. Once the file is clean, mark it as resolved and finish the merge:

git add path/to/file
git commit

If you want to check, at any point, which files still have unresolved conflicts, git status lists them under “both modified.”

Putting work aside without losing it: git stash

You’re mid-edit on some files but need to switch branches for something urgent, and you’re not ready to commit. git stash saves your local changes aside and resets your working directory to the last commit. Once the urgent thing is handled, you can bring them back:

git stash list
git stash pop

git stash list shows everything you’ve stashed, with the most recent as stash@{0}. git stash pop reapplies the latest one and removes it from the list. If you’d rather keep a copy in the stash just in case, use git stash apply instead: it reapplies the changes without deleting them from the stash.

The ultimate safety net: git reflog

Even after an overly hasty reset --hard, not everything is lost locally. Git keeps a log of every move HEAD makes on your machine, called the reflog:

git reflog

Each line corresponds to a state HEAD passed through, with a reference like HEAD@{2} (“where HEAD was two moves ago”). Once you’ve spotted the line right before your mistake, you can jump back to it:

git reset --hard HEAD@{2}

The reflog is local to your machine and isn’t shared with the remote: it’s your personal spare wheel, not to be confused with the history everyone else can see.

Switching branches cleanly: git switch

To move from one branch to another, the modern, unambiguous command is git switch:

git switch branch-name

To create a new branch and move onto it in a single command:

git switch -c new-branch

git switch was introduced to take over, specifically, from the older git checkout, which used to handle both switching branches and restoring files — two different jobs mixed into one command, and a common source of confusion. Today, git switch handles branches and git restore handles files, each with one clear job.

What you should never do: force-pushing a shared branch without knowing why

Here’s the single most important rule in this article. When you run git push, Git normally refuses to overwrite the remote history if your local history isn’t a direct continuation of it. The --force flag disables that protection. The concrete risk, spelled out in the official documentation: if you and somebody else both started from the same commit, and that person already pushed their own commits, a --force push from you can wipe their commits off the remote, because everyone now starts building on top of your version instead.

In practice: never run git push --force on a branch other people use, unless you know exactly what you’re doing and you’ve told them first. On a branch that’s entirely yours (say, your own feature branch nobody else has pulled), it’s harmless. If you do need to force-push a shared branch for a good reason (a coordinated rebase, for instance), prefer --force-with-lease: it checks that nobody has pushed new commits since your last sync before overwriting anything, which keeps you from accidentally erasing someone else’s work.

Key takeaways

  • Commit = a snapshot of your project; branch = a movable pointer; remote = a copy hosted elsewhere (origin).
  • Undo the last commit while keeping the changes: git reset HEAD~1.
  • Restore one specific file: git restore path/to/file (or --source=HEAD~2 to go further back).
  • Resolve a conflict by editing the file, removing the <<<<<<< / ======= / >>>>>>> markers, then git add + git commit.
  • Stash without losing anything: git stash, then git stash pop or git stash list.
  • The local safety net: git reflog, then git reset --hard HEAD@{n}.
  • Switch branches: git switch branch-name (or -c to create one).
  • Never run git push --force on a shared branch without being sure and without telling people; --force-with-lease is safer.

Sources

  1. git-reset Documentation — git-scm.com · accessed 27 September 2026
  2. git-restore Documentation — git-scm.com · accessed 27 September 2026
  3. git-stash Documentation — git-scm.com · accessed 27 September 2026
  4. git-reflog Documentation — git-scm.com · accessed 27 September 2026
  5. git-push Documentation — git-scm.com · accessed 27 September 2026
  6. Pro Git — Basic Branching and Merging — git-scm.com · accessed 27 September 2026

Read the next article

Explainer27 September 2026

Chinese AI: DeepSeek, Qwen, Kimi… why Europe is wary

Health27 September 2026

AI in medical school: study for PASS, LAS and the EDN safely

Tools27 September 2026

Free AI for students: every offer and discount (2026)

Career27 September 2026

AI on an internship or apprenticeship: what’s allowed, what isn’t

Code27 September 2026

From IDE to ADE: coding with AI agents in 2026

Code27 September 2026

Reading an error without panicking: the anatomy of a stack trace (Python and JavaScript)

Tools27 September 2026

Best AI for students in 2026: the honest comparison

Tools27 September 2026

New AI models in 2026: which one should you study with?

Tools27 September 2026

French AI tools you’ve never heard of: Noota, Moshi, Vibe…

Analysis27 September 2026

Why AI is so expensive (and American AI even more so)

Code27 September 2026

How to prompt an AI coding tool well: the method that changes everything

Code27 September 2026

Securing a vibe-coded app: 7 mistakes to fix before you publish

Code27 September 2026

Slopsquatting: when AI recommends packages that don't exist

Weekly brief27 September 2026

AI news roundup: the week of September 21–27, 2026

Code27 September 2026

Vibe coding: what it actually means (and how not to mess it up)

AI news27 September 2026

Why Yann LeCun wants AMI: AI beyond LLMs

Tutorial26 September 2026

Connect an MCP connector to Claude without writing a line of code

Degrees26 September 2026

Choosing your program with real MonMaster and InserSup data

Method26 September 2026

APA 7, ISO 690, Vancouver: how to cite your sources properly

Explainer26 September 2026

ChatGPT's 'hidden codes' on TikTok: fact vs. fiction

Health26 September 2026

Medicine: revise for the EDN with France's public drug database

Career26 September 2026

Internship pay and apprentice wages in 2026: the rules

Explainer26 September 2026

AI and academic integrity: what universities actually say

Tutorial26 September 2026

Installing a skill in Claude in 2 minutes

Thesis26 September 2026

Thesis: building your research question and outline with AI

Method26 September 2026

Building your exam study schedule with AI, the right way

Method26 September 2026

Revising with AI, honestly: active recall, Feynman, quizzes

Career26 September 2026

Choosing your apprenticeship with real employment data

Code27 September 2026

Learning to code in the age of AI: what you still need to know how to do yourself

Code27 September 2026

The one-page spec to write before you prompt an AI to code

Thesis27 September 2026

How to cite ChatGPT or Claude in a thesis: APA, MLA, ISO 690

Code27 September 2026

Claude Code for beginners: install, first launch, CLAUDE.md

AI news27 September 2026

Claude Opus 5.5: what really changes (and how to use it to study)

Analysis27 September 2026

The AI race: why some people are scared and others aren't

Code27 September 2026

Building your first MCP server, step by step

Code27 September 2026

Deploying your first site for free: Vercel, Netlify, Cloudflare Pages, or GitHub Pages

Explainer27 September 2026

AI detectors: are Turnitin, GPTZero and Compilatio reliable?

Code27 September 2026

Writing your own Claude skill: structure, SKILL.md, and a description that triggers

Career27 September 2026

France's national student-entrepreneur status (SNEE) and the PEPITE network, explained

Analysis27 September 2026

France in the AI race: Mistral, energy and talent

AI news27 September 2026

French Tech and AI: the French startups to know in 2026

Join GetPack

Already have an account? Sign in