Elena' s AI Blog

Git Checkout for overwriting directories from different branches

22 Sep 2024 / 4 minutes to read

Elena Daehnhardt


Git checkout to overwrite a directory contents


Introduction

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.

For this, we can use the versatile Git checkout command with caution since this with totally rewrites your files in the destination branch.

What is Git checkout?

Let’s start with the main functionality of the Git checkout command, which lets us navigate through your project’s history and work on different versions of code, which can be represented by branches, commits, or even specific files.

We can use the ‘git checkout’ command for (see git-checkout documentation for more):

  1. 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.
  2. 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.
  3. 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.

Git checkout to overwrite the files

OK, here’s how to overwrite only the “/scripts” directory from the ‘publish’ branch to the ‘master’ branch in Git:

  1. Checkout the Master Branch:

    git checkout master
    
  2. Replace the “/scripts” directory with the one from ‘dev’

    git checkout dev -- scripts/
    
  3. Commit the Changes:

    git commit -m "Overwritten /scripts directory in master with dev content"
    

You will also see that files which were not yet present in the destination directory are created:

[master abe8711e] Overwritten /scripts directory in master with dev content
 3 files changed, 11 insertions(+), 2 deletions(-)
 create mode 100744 scripts/publish/how_to.md
  1. (Optional) Push the Changes:

    git push origin master 
    

Conclusion

In short, ‘git checkout’ is a fundamental Git command that lets you navigate your project’s history and work on different code versions. We have learned about a simple Git checkout recipe to replace file contents in a required directory with the contents from another branch. It is easy, but use it with caution. Good luck!

References

  1. Restoring deleted files in Git

  2. git-checkout

Did you like this post? Please let me know if you have any comments or suggestions.

Git posts that might be interesting for you




desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.

Citation
Elena Daehnhardt. (2024) 'Git Checkout for overwriting directories from different branches', daehnhardt.com, 22 September 2024. Available at: https://daehnhardt.com/blog/2024/09/22/edaehn-git-overwrite-directory-with-contents-from-a-branch/
All Posts