JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Copy files from another branch in Git

Author:JIYIK Last Updated:2025/04/03 Views:

In Git, merging various files can lead to a lot of conflicts. Through these merge conflicts, our files may be compromised, so we have to copy these files or folders from one branch to another to keep them safe.

A popular approach is cherry picking. But if we don't want to do that, we have a better approach: branching from a remote git checkout.


Use the git checkout command to copy files from another branch in Git

The git checkout command is used to change branches and restore files in the working tree. It is also used to operate files, folders, and commits.

This article will show you how to use git checkoutthe command to copy single or multiple folders or files from one branch to another without merging the entire branch with the other branch.

Following are the commands we can use to copy files from other branches. It depends on where we want to get the files from (local branch, commit or remote branch).

We can git statuscheck which branch we are on by running the command:

$ git status

After that, we will create a file and commit it to another branch.

$c git checkout -b new_branch

$ git add test.txt

$ git commit -m "Create test"

Now, we will switch to the master branch again.

$ git checkout master

We will check out the files from the other branch to copy the files.

$ git checkout new_branch test.txt

Finally, our file is successfully copied to our current branch. Therefore, we can check it using the following command.

$ git status

Use the git checkout command to copy one or more files from another branch in Git

For single or multiple files we will run the following command:

$ git checkout <other-branch-name> -- path/to/your/file.

Use the git checkout command to copy a folder from another branch in Git

To copy this entire folder to our current branch, we would execute the following command:

git checkout <other-branch-name> -- path/to/your/folder

Use the git checkout command to copy files and folders from a commit in another branch.

To copy some files or folders from a specific commit in another branch, we would run this command:

git checkout <commit_hash> <relative_path_to_file_or_dir>

Previous:Push to a specific branch in Git

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Push to a specific branch in Git

Publish Date:2025/04/02 Views:54 Category:Git

In Git, we use branches to develop independent features directly from the main workflow of the project. Since Git is considered to be the best version control system so far, we have local and remote branches in our repository for different

Solve the Git Push Everything Up-To-Date Problem

Publish Date:2025/04/02 Views:120 Category:Git

Git is a free, open source version control system designed to work with projects quickly and efficiently. You can make changes to your repo and push them to master branches. This article explains how to use git push the command to resolve e

Git merge development branch into feature branch

Publish Date:2025/04/02 Views:127 Category:Git

Creating new branches and merging them is considered to be the most powerful tool of Git. The ability of Git to create new branches and merge them into the development code is very useful for developers working in a team environment. This f

Understanding Git conflict markers

Publish Date:2025/04/02 Views:113 Category:Git

In this article, we will discuss git conflict markers. Understanding Git conflict markers When pulling changes from a remote repository, you may encounter merge conflicts. Merge conflict files can sometimes be confusing. A typical merge con

Selectively merge changes from different branches in Git

Publish Date:2025/04/02 Views:117 Category:Git

This article will discuss merging specific changes from one branch to another. As we know, when merging branches, Git merges all files without exception. You may find yourself in a scenario where you have some commits in one branch and you

Complete the merge after resolving conflicts in Git

Publish Date:2025/04/02 Views:63 Category:Git

This article describes the process of completing a merge after resolving merge conflicts in Git. We will go through the merge steps, resolve the conflicts, and complete the merge. Complete the merge after resolving conflicts in Git For a si

Merge the development branch into Master in Git

Publish Date:2025/04/02 Views:168 Category:Git

This article provides an overview of merging a development branch into the master branch. Often we find ourselves creating a branch outside of the master branch for development. Once we are happy with the changes, we can merge them into mas

Recovering a conflicting Git merge

Publish Date:2025/04/02 Views:161 Category:Git

This article explains the revert command when a merge conflict occurs git merge . We will also take a quick look at how to undo a git merge that was successful and has been pushed to a remote repository. Recovering a conflicting Git merge I

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial