Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

git commit changes to different branch

git stash
git checkout other-branch
git stash pop
Comment

commit changes from different branches

#Solution 1: Use the git checkout command
#The git checkout command offers a simple way to get a file or a folder from another branch.

#Here is the syntax to checkout a file from another branch:

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

#1. Checkout to the branch where you want to copy the file.

git checkout feature/A

#2. Once you are on the correct branch, copy the file.

git checkout feature/B -- utils.js

#3. Use the git status command to ensure that the file has been copied.

#4. Commit and push to a remote.

#When using the checkout command, you can also get:

#A folder from another branch.
#Multiple files by specifying each one of them.
#Also, note that you can get a file/folder from the stash.

#Solution 2: Use the git restore command
#Another option is to use the git switch command with the git restore command.

#If you have never heard about those two commands, that's alright. They are relatively new. Git introduced them in version 2.23 in 2019.

#The purpose of those two commands is to split up the git checkout command's responsibilities to simplify it for users.

#The git restore command restores the working tree.

#The git switch command switches branches.

#Here is the process to follow to get a file from another branch:

#1. Switch to the branch where you want to checkout the file.

git switch feature/A

#2. Get the file from the other branch.

git restore --source feature/B -- utils.js

#3. Commit and push the changes.

#Solution 3: Use the git show command
#Finally, we can use the git show command.

#Here is the process to follow:

#1. Switch to the working branch.

git switch feature/A

#2. Get the file from the other branch.

git show feature/B:path/utils.js > path/utils.js

#3. Commit and push the changes.

#Note: You need to specify the relative path from your directory's root this time.
Comment

PREVIOUS NEXT
Code Example
Shell :: how to push folder into private repo github 
Shell :: ionic capacitor v3 add android 
Shell :: pesquisar git 
Shell :: ecto migration 
Shell :: git fatal pack has bad object at offset 
Shell :: how to put value of one variable into another in bash 
Shell :: get wsl version 
Shell :: install rancher 
Shell :: powershell get all applications installed 
Shell :: how to check laravel installer version 
Shell :: pip install pygame 
Shell :: heroku rebuild without push 
Shell :: how to install scoop using powershell 
Shell :: microsoft-todo-unofficial ubuntu 
Shell :: git code push 
Shell :: install nano in docker 
Shell :: install react-player react 
Shell :: find unix 
Shell :: bash script getopts no argument 
Shell :: remove file from stage git 
Shell :: create symbolic link linux 
Shell :: -bash: /bin/rm: Argument list too long inodes 
Shell :: npm sequelize 
Shell :: virtualbox guest additions ubuntu 20.04 install 
Shell :: tar compress powershell 
Shell :: gnutls_handshake() failed: An unexpected TLS packet was received. 
Shell :: add job to crontab 
Shell :: get disk partitions linux 
Shell :: angular add component 
Shell :: psycopg2-binary install for M1 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =