# GitQuest: the full Git guides > The complete text of GitQuest's Git command guides, error fixes, workflow guides and blog articles, in English, as Markdown. GitQuest (https://www.gitquest.app) is a free, bilingual platform to learn Git by doing. Each guide below starts with its source URL. Short index: https://www.gitquest.app/llms.txt. French version: https://www.gitquest.app/fr/llms-full.txt. --- # Git for beginners: start here Source: https://www.gitquest.app/en/git-for-beginners/ Never used Git? Don't panic. This page explains everything from the start, simply, and shows you how GitQuest guides you step by step. ## What exactly is Git? When you work on a project (a website, an application, a code document), you're constantly modifying files. Sometimes you break something. Sometimes you want to go back. Sometimes you're working with other people on the same files. Git is the tool that solves these problems. It records every change you make (this is called a "commit"), which lets you navigate through your project's history like a timeline. With Git, you can also create "branches" to test ideas without risking the main project, then merge them when you're satisfied. ### The 3 key concepts of Git 1. The commit A "save point" of your project. You can return to it at any time. 2. The branch A parallel copy of your project to work on without risk. 3. The merge The fusion of two branches to combine everyone's work. ## Start with the free course The "Getting Started with Git" course is available online, for free, no sign-up required. 5 chapters, interactive exercises in the browser and a certificate of completion. ### 1. Install and start config, init, clone, GitHub ### 2. Track your changes status, add, diff, restore ### 3. Save your work commit, log, .gitignore, push ### 4. Branches branch, switch, merge ### 5. Conflicts Step-by-step resolution Free, no sign-up, directly in the browser ## An app to go further Beyond the online course, GitQuest offers a mobile app with investigations, daily challenges and gamified progression. Available on iOS and Android. ## Frequently asked questions about Git for beginners ### What is Git, in simple terms? Git is a tool that records the history of your files. Think of it as a supercharged "Ctrl+Z" that works on an entire project, lets you go back in time, work with others without overwriting each other's work, and keep track of every change. It's an essential tool for every developer. ### Is Git hard to learn? The basics of Git (init, add, commit, push) can be learned in a few hours. What's difficult is understanding the concepts behind the commands: the staging area, branches, merges. That's exactly what GitQuest makes tangible through interactive investigations. ### How long does it take to learn Git? With regular practice of 15 to 20 minutes a day, you can master the basics in 1 to 2 weeks. For advanced operations (rebase, cherry-pick, conflict resolution), count 4 to 6 weeks. GitQuest structures this progression for you. ### Do I need to install Git to use GitQuest? No, not to get started. GitQuest includes a terminal simulator that reproduces Git's behavior. You can learn directly in the app. When you're ready, we'll guide you through installing Git on your machine. ### What is the difference between Git and GitHub? Git is the command-line tool that manages file versions on your machine. GitHub is an online platform that hosts your Git projects and facilitates collaboration. GitQuest teaches you Git first, then you'll naturally see how to use GitHub. ## Ready for your first commit? No installation needed. Start the free course directly in your browser. --- # Git Tutorial: from zero to mastery Source: https://www.gitquest.app/en/git-tutorial/ A complete and free course to learn Git step by step. From your first commits to advanced commands, with examples you can reproduce in your terminal. This tutorial teaches you Git from scratch. Each command is explained with a hands-on example you can copy-paste into your terminal. No account needed, no installation required to read — let's go. ## Level 1 — Beginner: the foundations of Git Git is a version control system. In practice, it records every change to your code in a history, like save points in a video game. If you break something, you can go back. If you work as a team, everyone can modify the code without overwriting each other's work. Here are the five commands to get started. ### git init — Create a repository Every Git project starts with `git init`. This command creates a hidden `.git/` folder that contains your project's entire history. You never touch this folder directly — Git handles it. ```bash $ mkdir my-project && cd my-project $ git init Initialized empty Git repository in /my-project/.git/ ``` ### git add — The staging area Before saving, you need to tell Git which files to include. That's the role of the staging area. Think of it as a shopping cart: you place what you want to buy before going to the checkout. `git add` places a file in that cart. ```bash $ echo "Hello" > index.html $ git add index.html $ git status Changes to be committed: new file: index.html ``` ### git commit — Save a snapshot `git commit` takes everything in the staging area and creates a permanent snapshot. Each commit has a unique identifier (a hash like `a1b2c3d`) and a descriptive message. It's your save point. ```bash $ git commit -m "Add the home page" [main (root-commit) a1b2c3d] Add the home page 1 file changed, 1 insertion(+) ``` ### git status — Read the project state This is the command you'll use most often. `git status` shows you which files have been modified, which are in the staging area, and which are not yet tracked by Git. When in doubt, run `git status`. ```bash $ git status On branch main Changes not staged for commit: modified: index.html Untracked files: style.css ``` ### git log — Navigate the history `git log` displays the list of commits from most recent to oldest. With the `--oneline` option, you get a compact view perfect for quickly spotting a commit. It's your logbook. ```bash $ git log --oneline c3d4e5f Add the contact form b2c3d4e Update CSS styles a1b2c3d Add the home page ``` Mastered the five basic commands? Great. Let's move on to branches — that's where Git becomes truly powerful. ## Level 2 — Intermediate: branches and collaboration Branches let you work on multiple features in parallel without stepping on each other's toes. Think of a tree: the trunk is your `main` branch, and each branch is a feature being developed. When it's ready, you merge it back into the trunk. ### git branch / git switch — Create and navigate `git branch name` creates a new branch. `git switch name` moves you onto it (you may also see the older syntax `git checkout`, but `switch` is clearer). Each branch is an isolated workspace. ```bash $ git branch feature/contact $ git switch feature/contact Switched to branch 'feature/contact' $ git branch main * feature/contact ``` ### git merge — Merge a branch When your feature is complete, you go back to `main` and merge your branch with `git merge`. If nobody has modified the same files, Git does a fast-forward merge — it's automatic and clean. ```bash $ git switch main $ git merge feature/contact Fast-forward merge. contact.html | 1 + 1 file changed, 1 insertion(+) ``` ### Merge conflicts: understanding and resolving When two branches modify the same line of the same file, Git can't guess which version to keep. It flags a conflict. Don't panic: Git marks the conflicting areas in the file with `<<<<<<<` and `>>>>>>>` markers. You choose the right version, remove the markers, then do an `add` + `commit`. Terminal (the conflict): ```bash $ git merge feature/header CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit. ``` Terminal (the resolution): ```bash # After manually resolving the file: $ git add index.html $ git commit -m "Resolve conflict in index.html" [main f6g7h8i] Resolve conflict in index.html ``` ### git push / git pull — Sync with a remote `git push` sends your commits to a remote server (GitHub, GitLab...). `git pull` fetches other people's commits and merges them into your branch. It's the basic duo of collaboration. ```bash $ git push origin main Enumerating objects: 5, done. To github.com:user/my-project.git a1b2c3d..f6g7h8i main -> main ``` ### git clone — Get an existing project `git clone` copies a remote repository to your machine, along with its entire history. It's the first command you run when joining an existing project. ```bash $ git clone https://github.com/user/my-project.git Cloning into 'my-project'... Receiving objects: 100% (42/42), done. ``` You know how to create branches, merge and collaborate? You already have an operational skill level for working in a team. To go further, here are the pro tools. ## Level 3 — Advanced: the pro toolkit These commands aren't essential every day, but they'll get you out of tricky situations and save you valuable time. It's what separates someone who uses Git from someone who masters it. ### git rebase — Rewrite history `git rebase` takes the commits from your branch and replays them on top of another branch. The result: a linear history with no merge commits. It's cleaner, but should only be used on branches you haven't shared yet (`push`). ```bash $ git switch feature/login $ git rebase main Rewinding head to replay your work on top of it... Applying: Add the login form ``` ### git stash — Set aside temporarily Working on a feature but need to switch branches urgently? `git stash` puts your changes aside in a temporary stack. You switch branches, do what you need to do, then `git stash pop` restores your work. ```bash $ git stash Saved working directory and index state WIP on main $ git stash pop On branch main — Changes restored ``` ### git cherry-pick — Copy a specific commit Need to grab a single commit from another branch without merging everything? `git cherry-pick` copies that exact commit into your current branch. Ideal for applying a targeted fix. ```bash $ git cherry-pick a1b2c3d [main x9y8z7w] Fix the critical bug 1 file changed, 3 insertions(+), 1 deletion(-) ``` ### git reflog — The safety net Did an overly aggressive `reset` and think you lost commits? `git reflog` records everything Git has done, even destructive operations. You can find any previous state and go back to it. It's Git's ultimate `Ctrl+Z`. ```bash $ git reflog f6g7h8i HEAD@{0}: commit: Add the footer a1b2c3d HEAD@{1}: reset: moving to HEAD~1 e4f5g6h HEAD@{2}: commit: Important work (lost?) $ git checkout e4f5g6h Commit recovered! ``` ### git reset / git revert — Undo changes Two approaches to undo. `git reset` moves the branch pointer backwards — commits disappear from history (use `--soft` to keep the files). `git revert` creates a new commit that undoes the changes — history stays intact. When working in a team, prefer `revert`. ```bash # Undo the last commit (keep files) $ git reset --soft HEAD~1 # Or create a reverse commit (clean history) $ git revert HEAD [main z1y2x3w] Revert "Add the footer" ``` ### git bisect — Find the faulty commit A bug appeared but you don't know when? `git bisect` does a binary search through your history. You give it a "good" commit and a "bad" commit, and Git has you test the middle commit. In a few steps, you find exactly the commit that introduced the bug. ```bash $ git bisect start $ git bisect bad $ git bisect good a1b2c3d Bisecting: 5 revisions left to test (roughly 3 steps) # Git automatically tests the middle commit... $ git bisect good d4e5f6g is the first bad commit ``` There you have it — a complete overview of essential Git commands, from beginner to advanced level. The key to improving: practice regularly. Create a test project and experiment with each command. ## Want to practice these commands? GitQuest turns learning Git into interactive investigations. Every command in this tutorial becomes a hands-on exercise in a simulated terminal — right on your phone. ## Questions about this Git tutorial ### Where should I start to learn Git? Start with the three basic commands: git init (create a repository), git add (stage files) and git commit (save). This tutorial guides you step by step from these foundations to advanced commands. ### What is the difference between git add and git commit? git add stages the files you want to save (it puts them in the "staging area"). git commit creates the actual save (a "restore point"). It's a two-step process that lets you choose precisely what to include in each save. ### Should I learn Git on the command line or with a GUI? Start with the command line. It's the universal method that works everywhere. Once you've mastered the concepts, you can use any GUI (VS Code, GitKraken, etc.) while understanding what's actually happening under the hood. ### What is the difference between git merge and git rebase? git merge creates a merge commit that preserves the full history of both branches. git rebase rewrites history by replaying your commits on top of another branch, resulting in a cleaner linear history. Use merge for shared branches, rebase for your local branches. ### How long does it take to learn Git? The basics (init, add, commit, status, log) can be learned in a few hours. Branches and collaboration in a few days. Advanced commands (rebase, cherry-pick, bisect) require regular practice over a few weeks. ### Are Git and GitHub the same thing? No. Git is the version control tool that runs on your machine. GitHub is an online platform that hosts Git repositories and adds collaboration features (pull requests, issues, etc.). GitLab, Bitbucket and other alternatives also exist. --- # Git vs GitHub: what is the difference? Source: https://www.gitquest.app/en/git-vs-github/ Git and GitHub are often confused. One is a tool installed on your machine, the other is an online service. Here is how to clearly distinguish them. ## The comparison at a glance The tool - + Software installed on your machine - + Works offline - + Manages your code history (versioning) - + Created by Linus Torvalds in 2005 - + Open source and free - + Commands: `init, add, commit, branch, merge` The platform - + Online service (website) - + Requires an internet connection - + Hosts your Git repositories online (sharing) - + Created in 2008, acquired by Microsoft in 2018 - + Free (with paid options) - + Features: pull requests, issues, CI/CD, pages ## In practice: Git then GitHub ### Git (local) You work on your machine: you create a repository, make commits, manage your branches. All without internet. Terminal (git local): ```bash # Git = local tool on your machine $ git init my-project Initialized Git repository in /my-project/.git/ $ git add index.html $ git commit -m "First commit" [main (root commit)] First commit # Everything happens locally, no internet needed ``` ### GitHub (online) When you want to share your code or collaborate, you push your repository to GitHub. Your colleagues can then pull it and contribute. Terminal (github): ```bash # GitHub = online platform for sharing $ git remote add origin https://github.com/user/project.git $ git push -u origin main Branch 'main' set up to track 'origin/main' Your code is now visible on GitHub! # Collaboration: pull requests, issues, CI/CD ``` ## The simple analogy To understand the difference, think of this analogy: ### Git = Word It's the software on your machine. You create, edit and save your work locally. ### GitHub = Google Drive It's the online service. You upload your file to share it with others and collaborate. ### Both together You work locally with Git, and sync with GitHub to share and collaborate. ## What about alternatives to GitHub? ### GitHub 100M+ users. The reference for open source. Profiles visible to recruiters. Owned by Microsoft. ### GitLab CI/CD built in natively. Popular in enterprises. Can be self-hosted. Generous free tier. ### Bitbucket Integrated with Jira and Confluence. Popular with teams using the Atlassian ecosystem. Free for small teams. Good news: Git commands are the same regardless of the platform. Learn Git once, use it everywhere. ## Questions about Git and GitHub ### Can you use Git without GitHub? Yes, absolutely. Git works entirely locally on your machine. You can create repositories, make commits, create branches, all without an internet connection and without GitHub. GitHub is optional: it's an online service that makes sharing and collaboration easier. ### Can you use GitHub without knowing Git? Technically yes (GitHub has a web interface), but you'd be very limited. To contribute effectively to projects on GitHub, you need to master basic Git commands: clone, add, commit, push, pull, branch, merge. ### Is GitHub the only service of its kind? No. GitLab and Bitbucket are the two main alternatives. They offer similar features (repository hosting, CI/CD, collaboration). GitHub is the most popular, especially for open source, but Git skills transfer to all these platforms. ### Is GitHub free? Yes, for most use cases. Public and private repositories are free, with limitations on some advanced features (CI/CD minutes, storage). Paid plans add features for teams and enterprises. ### Why does everyone talk about GitHub for recruiters? GitHub has become the technical portfolio for developers. Recruiters check GitHub profiles to see projects, contribution regularity, code quality and open source contributions. It's a professional showcase. --- # Git commands cheat sheet: the essential list Source: https://www.gitquest.app/en/git-cheat-sheet/ The Git commands you will use every day, sorted by usage: setup, commit, branches, remote, history and undo. Click a command to see it explained with examples. This list covers the most commonly used Git commands. Each linked command opens a full guide with syntax, examples and common mistakes. For definitions, see the full Git glossary. ## Start a project `git init` Initialize a new Git repository `git clone ` Clone a remote repository `git status` View the state of files `git status -s` Short status, one line per file ## Save changes `git add ` Add a file to staging `git add .` Add all modified files `git commit -m "message"` Create a commit `git commit --amend` Edit the last commit (message or content) `git rm ` Delete a file and stage the deletion `git mv ` Rename or move a tracked file `git diff` View unstaged changes `git diff --staged` View staged changes ## Work with branches `git branch` List branches `git branch ` Create a new branch `git checkout ` Switch branches `git switch ` Switch branches (modern) `git checkout -b ` Create and switch to a branch `git branch -d ` Delete a merged branch `git branch -m ` Rename the current branch `git worktree add ` Check out a branch in another folder ## Merge and rebase `git merge ` Merge a branch into the current one `git rebase ` Rebase onto a branch (linear history) `git rebase -i HEAD~n` Interactive rebase on n commits `git merge --abort` Abort a merge in progress `git cherry-pick ` Apply a specific commit ## Work with a remote repository `git remote -v` List remote repositories `git remote add origin ` Connect a remote repository `git push` Push commits to the remote `git push -u origin ` Push and set up tracking `git push --force-with-lease` Force push safely after a rebase `git pull` Fetch and merge from the remote `git fetch` Fetch without merging ## View history `git log` View commit history `git log --oneline` Condensed history (one line per commit) `git log --graph` History with branch graph `git show ` Details of a specific commit `git blame ` Who modified each line `git shortlog -sn` Count commits per author `git grep "text"` Search text in tracked files `git bisect start` Find the commit that introduced a bug `git reflog` History of all HEAD movements ## Undo and fix `git restore ` Discard changes to a file `git restore --staged ` Unstage a file `git reset --soft HEAD~1` Undo last commit (keep changes) `git reset --hard HEAD~1` Undo last commit (discard everything) `git revert ` Create a reverse commit (safe) `git stash` Set aside current changes `git stash pop` Restore stashed changes `git stash list` List stashed changes `git clean -fd` Delete untracked files and folders ## Configuration `git config --global user.name "Name"` Set your name `git config --global user.email "email"` Set your email `git config --list` Show all your Git settings `git tag ` Create a tag (e.g., v1.0.0) `git apply ` Apply a patch file --- # Git glossary: all commands explained Source: https://www.gitquest.app/en/git-glossary/ Over 30 Git commands explained simply, with their syntax and a clear description. Your reference guide for learning and reviewing. This glossary gathers the essential Git commands, organized by category. Each command is explained with its syntax and a summary of what it does. New to Git? Start with the Basics section. Looking for a specific command? Browse the categories below. And to practice each command in context, try the GitQuest investigations. ## Basics ### git init `git init [project-name]` Initializes a new Git repository in the current directory or in a new folder. ### git clone `git clone ` Clones a remote repository to your local machine. Copies the entire history and all branches. ### git add `git add ` Adds files to the staging area (index). Prepares changes for the next commit. ### git commit `git commit -m "message"` Records staged changes in the repository history with a descriptive message. ### git status `git status` Displays the state of the working directory and staging area. Shows modified, added or untracked files. ### git log `git log [--oneline]` Displays the commit history. The --oneline option condenses each commit to a single line. ### git diff `git diff [file]` Shows differences between the working directory and the staging area, or between two commits. ### git rm `git rm ` Removes a file from both the working directory and the staging area. ### git mv `git mv ` Renames or moves a file and records the change in Git. ## Branches ### git branch `git branch [name]` Lists existing branches or creates a new branch without switching to it. ### git checkout `git checkout ` Switches branches or restores files. Also used to create and switch with -b. ### git switch `git switch ` Switches branches (newer and clearer command than checkout for this use case). ### git merge `git merge ` Merges the specified branch into the current branch. May create a merge commit. ### git rebase `git rebase ` Replays commits from the current branch onto the target branch. Produces a linear history. ### git cherry-pick `git cherry-pick ` Applies a specific commit to the current branch without merging the entire source branch. ### git branch -d `git branch -d ` Deletes a local branch that has already been merged. Use -D to force deletion. ## Remote repositories ### git remote `git remote -v` Displays configured remote repositories and their URLs (fetch and push). ### git push `git push [origin] [branch]` Sends local commits to the remote repository. Publishes your work for the team. ### git pull `git pull [origin] [branch]` Fetches changes from the remote repository and merges them into your local branch. ### git fetch `git fetch [origin]` Fetches changes from the remote repository without merging. Lets you see what changed before integrating. ### git remote add `git remote add ` Adds a new remote repository with a short name (usually origin). ### git push -u `git push -u origin ` Pushes a branch and sets up tracking. Future push/pull commands won't need to specify the branch. ## Advanced commands ### git stash `git stash [pop|list|drop]` Sets aside current changes without committing. Useful for switching branches quickly. ### git reset `git reset [--soft|--mixed|--hard] ` Moves HEAD to a previous commit. Depending on the mode, keeps or discards changes. ### git revert `git revert ` Creates a new commit that undoes the changes of a previous commit. Safer than reset. ### git reflog `git reflog` Displays the history of all HEAD movements. Allows you to find "lost" commits. ### git bisect `git bisect start` Binary search for the commit that introduced a bug. Git tests commits one by one. ### git tag `git tag [commit]` Creates a label on a commit to mark a version (e.g., v1.0.0). ### git blame `git blame ` Shows who modified each line of a file and in which commit. ### git show `git show ` Displays the details of a commit: author, date, message and diff of changes. ### git clean `git clean -fd` Removes untracked files from the working directory. Warning, this action is irreversible. ### git config `git config --global user.name "Name"` Configures Git (name, email, editor, aliases...). --global for all projects, --local for the current project. ## A glossary is good. Practice is better. Knowing a command's syntax isn't enough. To truly master Git, you need to practice in concrete situations. That's exactly what GitQuest offers: interactive investigations where every command gains meaning. --- # Git add: stage files for your next commit Source: https://www.gitquest.app/en/git-add-explained/ Before committing, you need to tell Git which files to include. git add places your changes in the staging area, ready for the next commit. ## What is git add? `git add` is the bridge between your working directory and the next commit. It places changes in the staging area (also called the index). Think of it like shopping: you put items in your cart (`git add`) before checking out (`git commit`). You can add or remove items as many times as you want before confirming. Without `git add`, your changes will not be included in the next commit. It's the most fundamental command after git init. ### The Git workflow in 3 steps `Working Directory` Your local files, modified or new ↓ `git add` `Staging Area (Index)` Files ready to be committed ↓ `git commit` `History` Commits permanently recorded ## Git add in practice Add specific files or the entire directory. ### Add specific files Terminal (git add): ```bash # Add files to the staging area $ git status modified: index.html new file: style.css $ git add index.html style.css $ git status modified: index.html new file: style.css (use "git restore --staged ..." to unstage) ``` ### Add all or by pattern Terminal (git add advanced): ```bash # Add all modified files $ git add . All modified files added to staging # Or add by pattern $ git add *.css CSS files added to staging # Add interactively $ git add -p diff --git a/index.html b/index.html Stage this hunk [y,n,q,a,d,e,?]? ``` ## Essential add commands `git add file` Stage a specific file `git add .` Stage the entire current directory `git add -A` Stage everything (including deletions) `git add -p` Interactive patch-by-patch mode `git add *.ext` Stage by pattern (glob) `git add -u` Stage tracked modified files ## Common mistakes with git add ### git add . without a .gitignore You risk adding node_modules, .env, build files... Always create a .gitignore before your first git add. ### Forgetting git add before git commit If you commit without add, nothing happens. Git only commits what is in the staging area. ### Confusing add and commit git add does not save anything to the history. It's git commit that creates the permanent snapshot. Part of the Git Basics guide ## Questions about git add ### What is the difference between git add and git commit? git add stages files (staging area), git commit permanently records the staged files in the project history. Add = put items in your cart, commit = place the order. ### What does git add . (with a dot) do? The dot means "the entire current directory". git add . stages all modified, new, and deleted files in the current directory and its subdirectories. ### Can you undo a git add? Yes, with git restore --staged file or git reset HEAD file. The changes remain in your working directory, they are just removed from staging. ### Do I always need to run git add before git commit? Yes, unless you use git commit -a which automatically stages already-tracked modified files. But new files always need to go through git add first. ### What is the staging area? It is an intermediate zone between your working directory (your files) and the Git history. It lets you choose exactly what you want to include in your next commit. --- # Git apply: apply a patch to your code Source: https://www.gitquest.app/en/git-apply-explained/ git apply file.patch applies the changes from a patch (a diff file) to your working directory, without creating a commit. Check it first with git apply --check. Ideal for sharing fixes without a branch or pull request. ## What is git apply? `git apply` reads a patch file (a diff in text format) and applies the changes to your code. It is like copy-pasting a diff automatically. Unlike git am, apply does not create a commit. The changes are applied to the working directory and you decide when to commit. ### Git apply syntax `git apply file.patch` Apply a patch `git apply --check file.patch` Verify without applying `git apply -R file.patch` Undo a patch (reverse) ## Git apply in practice Applying and verifying patches. ### Applying a patch Terminal (git apply): ```bash # Apply a patch to the code $ git apply fix-header.patch Patch applied successfully $ git status modified: src/header.js modified: src/style.css # Changes are in the working directory (not staged) $ git add . && git commit -m "fix: apply header patch" [main a1b2c3d] fix: apply header patch ``` ### Verify and undo Terminal (git apply --check): ```bash # Check if the patch applies without errors $ git apply --check fix-header.patch (no output = the patch applies correctly) # View patch stats without applying it $ git apply --stat fix-header.patch src/header.js | 5 +++-- src/style.css | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) # Apply in reverse (undo a patch) $ git apply -R fix-header.patch Patch reversed ``` ## git apply vs git am ### `git apply` Applies a raw diff to the working directory. No automatic commit. You stay in control. For: simple patches (git diff > file.patch) ### `git am` Applies an email-formatted patch and automatically creates a commit with the original author. For: patches from git format-patch ## Common mistakes with git apply ### The patch does not apply The code has changed since the patch was created. Try `git apply --3way` for automatic resolution, or apply the changes manually. ### Applying a format-patch with apply Patches from format-patch contain email metadata. Use `git am` to apply them correctly with author and commit message. ### Forgetting to verify with --check Always run `git apply --check` first to verify that the patch applies cleanly before modifying your files. Part of the Git Advanced guide ## Questions about git apply ### What is a patch in Git? A patch is a text file that contains the differences (diff) between two states of the code. It describes added, removed, and modified lines. You can create one with git diff > file.patch or git format-patch. ### What is the difference between git apply and git am? git apply applies a raw diff (created with git diff). git am applies an email-formatted patch (created with git format-patch) that includes the commit message, author, and date. am automatically creates a commit. ### How do I verify that a patch applies correctly? Use git apply --check file.patch. If everything is OK, it outputs nothing. If there is a problem (conflict, missing file), it shows an error without modifying your files. ### What do I do if the patch does not apply? Use git apply --3way to attempt a 3-way merge resolution. If that does not work, the code has diverged too much since the patch was created. You will need to apply the changes manually. ### How do I undo an applied patch? If you have not committed yet, use git apply -R (reverse) file.patch. If you have already committed, use git revert to undo the commit. --- # Git archive: export your project cleanly Source: https://www.gitquest.app/en/git-archive-explained/ git archive creates an archive (zip, tar) of your project without the .git folder. Ideal for delivering code, creating releases, or sharing without history. ## What is git archive? `git archive` creates an archive of your project from a commit, tag, or branch. The archive contains your files without the .git folder. It is like packaging your code: you choose the version to package and Git creates a clean zip or tar, ready to be delivered or shared. ### Git archive syntax `git archive --format=zip -o f.zip HEAD` ZIP archive of HEAD `git archive --format=tar.gz -o f.tar.gz v1.0` tar.gz archive of a tag `git archive --prefix=project/ HEAD` With a root folder in the archive ## Git archive in practice Creating zip and tar archives of your project. ### ZIP archive Terminal (archive zip): ```bash # Create a ZIP archive of the project $ git archive --format=zip --output=project.zip HEAD Archive created: project.zip # Create an archive of a specific tag $ git archive --format=zip --output=v1.0.zip v1.0.0 Archive created: v1.0.zip # The .git folder is NOT included! ``` ### TAR archive and subdirectory Terminal (archive tar): ```bash # Create a tar.gz archive $ git archive --format=tar.gz --output=project.tar.gz HEAD Archive created: project.tar.gz # Export only a subdirectory $ git archive HEAD src/ | tar -x -C /tmp/export/ src/ directory exported # With a directory prefix $ git archive --prefix=my-project/ HEAD | gzip > release.tar.gz Archive with prefix created ``` ## git archive vs regular zip ### `git archive` Automatically excludes .git, respects export-ignore, can target a specific tag/commit. Recommended for Git projects ### `regular zip` Includes everything including .git, node_modules, and ignored files. Generic but less clean ## Common mistakes with git archive ### Including sensitive files If .env is in the repo, it will be in the archive. Use `.gitattributes` with export-ignore to exclude sensitive files. ### Forgetting --prefix Without prefix, files are at the root of the archive. Add `--prefix=project-name/` to put them in a folder (don't forget the trailing /). ### Archiving uncommitted files git archive only takes committed files. Uncommitted changes will not be in the archive. Commit or stash first. Part of the Git Advanced guide ## Questions about git archive ### Why use git archive instead of zip? git archive automatically excludes the .git folder, respects export rules (.gitattributes export-ignore), and can export a specific tag or commit. A regular zip includes everything, including the Git history. ### How do I exclude files from the archive? Add export-ignore in .gitattributes. For example: "tests/ export-ignore" excludes the tests folder from the archive. This is useful for excluding tests, CI, internal docs, etc. ### Can I archive a specific commit? Yes, replace HEAD with the commit hash or a tag: git archive --format=zip a1b2c3d. You can even archive a branch: git archive --format=zip origin/release. ### What formats are supported? Git supports tar, tar.gz (tgz), zip by default. The tar format is the lightest. Use --format to specify, or Git will infer it from the extension in --output. ### How do I archive only a subdirectory? Add the path after the reference: git archive HEAD src/. Only the contents of the src directory will be included in the archive. --- # Git bisect: find the faulty commit Source: https://www.gitquest.app/en/git-bisect-explained/ A bug appeared but you don't know when? git bisect uses a binary search to automatically find the commit that introduced the problem. ## How does bisect work? A binary search through your Git history. At each step, the search space is halved. ### 1. Start Start bisect and mark a good and a bad commit ### 2. Test Git places you in the middle. Test if the bug is present ### 3. Good/Bad Mark the commit as good or bad. Git halves the range ### 4. Found! After a few steps, Git identifies the faulty commit ## Git bisect in practice Start, test and find the faulty commit. ### Start the search Terminal (bisect start): ```bash # A bug appeared but you don't know when # Start the binary search $ git bisect start # Mark the current commit as bad (has the bug) $ git bisect bad # Mark an older commit as good (no bug) $ git bisect good v1.0.0 Bisecting: 12 revisions left to test after this (roughly 4 steps) [a3b4c5d] feat: add contact form ``` ### Test and find Terminal (bisect): ```bash # Git places you on a commit in the middle. Test it! # If the bug is present: $ git bisect bad Bisecting: 6 revisions left to test (roughly 3 steps) # If the bug is NOT present: $ git bisect good Bisecting: 3 revisions left to test (roughly 2 steps) # Repeat until the faulty commit is found f6g7h8i is the first bad commit commit f6g7h8i Author: dev@example.com fix: update price calculation # End the bisect session $ git bisect reset Back to original branch ``` ## Automate bisect with a script If the bug can be detected by an automated test, bisect can run on its own. `git bisect run npm test` Git runs `npm test` on each commit. If the test passes (code 0) = good. If the test fails = bad. Git finds the faulty commit automatically, without manual intervention. Part of the Git History, Search & Debug guide ## Questions about git bisect ### How does git bisect work? Git bisect uses a binary search. You provide a "good" and a "bad" commit, and Git places you in the middle. At each step you test and say whether it's good or bad. Git halves the search space each time, so even with 1000 commits, it takes roughly 10 steps. ### Can git bisect be automated? Yes! With git bisect run