Git Checkout Directory From Another Branch: How to Overwrite Its Contents
To checkout a directory (or folder) 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 git checkout does, the step-by-step version and the common pathspec error follow below.
What is git checkout?
π Subscribe to keep reading.
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.
You've hit a Deep Dive tutorial.
I spend dozens of hours researching, coding, and breaking things to write these guides. This content is free, but reserved for my subscriber community. Drop your email below to unlock this guide (and all past/future deep dives):
Full content temporarily unavailable β refresh in a moment
Already a subscriber? Use the magic link from your last newsletter, or reset your password.
Log in to unlock
New subscribers get an inbox mail: Set a password to unlock articles. The form does not log you in β use the same email afterwards.