When working on a feature branch, you may need to get the latest updates for specific directories from the main branch without merging or rebasing your entire branch.
To sync a specific directory from the main branch, use the following commands:
Copy
# First, fetch the latest changes from the main branchgit fetch origin main# Then, checkout just the directory you need from maingit checkout origin/main -- <directory-path>
Be careful when syncing directories that might have dependencies on other parts of the codebase. In some cases, you may need to sync multiple directories to maintain compatibility.
If you want to sync a directory but exclude certain files, you can:
First, sync the directory
Then revert the specific files you don’t want to sync
Copy
# First sync the entire directorygit checkout origin/main -- apps/docs# Then revert specific files you don't want to updategit checkout HEAD -- apps/docs/specific-file-to-exclude.mdx# You can also revert multiple files at oncegit checkout HEAD -- apps/docs/file1.mdx apps/docs/file2.mdx
For more complex selections, you can use Git’s interactive add mode after syncing:
Copy
# First sync the directorygit checkout origin/main -- apps/web# Then use interactive mode to selectively stage only what you wantgit add -i# Or use patch mode to select specific chunks within filesgit add -p
With the interactive mode, you’ll get a menu where you can:
Select specific files to stage
Review changes before staging
Split files into smaller chunks for selective staging