git push single file to another branch

To push a single file from your current branch to a different remote branch, you need to first incorporate that file into the target branch locally, then push the target branch to the remote. 

Here’s how to do it: Switch to the target branch.

    git checkout <target_branch_name>
  • Copy the file from the source branch:
    git checkout <source_branch_name> -- <path/to/file>

This command copies the specified file from source_branch_name into your current target_branch_name‘s working directory. Stage the changes.

    git add <path/to/file>

Commit the changes.

    git commit -m "Copied <file_name> from <source_branch_name> to <target_branch_name>"
  • Push the target branch to the remote:
    git push origin <target_branch_name>

This sequence of commands ensures that the specific file is integrated into the target_branch_name locally and then pushed as part of that branch’s history to the remote repository.

Leave a Reply