How to Overwrite a Directory from Another Git Branch with git checkout
To checkout a directory from another branch and overwrite its contents on your current branch, run git checkout <source-branch> -- <path>/ and commit the result. The same command also works to replace a single file with its version from another branch — for example, git checkout dev -- scripts/ replaces the scripts/ directory on your current branch with the version from dev, and git checkout dev -- path/to/file.py replaces just that one file.
Recently, I had to overwrite the “scripts” directory in my master branch with the files stored in the “scripts” directory of the “dev” branch. Here, I share the simplest way to overwrite the required directory completely with the respective directory contents from another branch.
Use this git checkout command with caution, since it completely rewrites your files in the destination branch.
What is git checkout?
git checkout is a Git command that updates the working directory to match a specified branch, commit, or file, letting you navigate your project’s history and work on different versions of code.
We can use the git checkout command for (see git-checkout documentation for more):
-
Switching Branches:
- The most common use of
git checkout is to navigate between the different branches you’ve created in your repository.
- When you check out a branch, Git updates the files in your working directory to match the version stored in that branch. It also tells Git to record all new commits on that branch.
-
Viewing Old Commits (Detached HEAD):
- You can also use
git checkout to view the state of your project at a specific commit in the past.
- This puts you in a “detached HEAD” state, meaning you’re not on any branch, and any new commits you make won’t be associated with a branch unless you create a new one.
-
Restoring Files:
git checkout can be used to discard changes in your working directory and revert a file to its state in the index (staging area) or a specific commit.
Please note that git checkout primarily affects the files in your working directory, making them match the version you’re checking out. This command also updates the HEAD pointer, which indicates the current branch or commit you’re working on.
Moreover, if you have uncommitted changes in your working directory when you check out a different branch or commit, Git will try to merge those changes. If there are conflicts, you’ll need to resolve them before completing the checkout.
For restoring files, Git also provides the git restore command, which offers more granular control over which parts of your project to restore. You can read about git restore in my post Restoring deleted files in Git.
Overwrite a Directory from Another Branch: Step-by-Step git checkout Commands
🔒 Subscribe to keep reading.
error: pathspec ‘dev’ did not match any file(s) known to git
🔒 Subscribe to keep reading.
Conclusion: git checkout Overwrites a Directory Path in One Command
🔒 Subscribe to keep reading.
References
🔒 Subscribe to keep reading.
Subscribe to unlock the full article ❤️
I keep most of the site completely open. A few unusually detailed tutorials need a free subscriber login so I can keep publishing this kind of work.
Log in to unlock
The form below signs you up for the newsletter. It does not log you into the app — log in afterwards (same email) to unlock this article and download your subscriber gifts. New subscribers get an inbox mail: Set a password to unlock articles.
Full content temporarily unavailable — refresh in a moment