This article will demonstrate how to get changes from another branch.
How to Get Changes From Another Branch?
To get changes from another branch, first, create a file in a local branch and do not add it to the Git staging area. Then, create and switch to another branch simultaneously. Check its status, track the file of the previous branch to the Git staging area into the new branch, and commit changes.
Try out the mentioned steps for a better understanding.
Step 1: Launch Git Bash Terminal
From the Windows Start menu, search the “Git Bash” terminal and open it:
Step 2: Navigate to Git Repository
Now, navigate to the required Git repository by running the given command:
Step 3: Create a File
To create a file, use the “touch” command and add the file name:
Step 4: Verify Created File
View the list of files to verify if the file is created or not:
Step 5: Check Git status
To view the current repository status, run the below-given command:
The below output signifies that our created file is untracked because we did not add it to the Git staging area:
Step 6: Check all Local Branches
Run the “git branch” command to view the list of local branch names:
It can be seen that the current repository contains three branches, and the asterisk “*” symbol beside the “alpha” branch refers to the current working branch:
Step 7: Switch to Another Branch
Switch to another branch with the help of the “git checkout” command:
In the below screenshot, you can see that we have switched from the “alpha” branch to the “dev” branch:
Step 8: View Current Branch List of Content
View the current branch list of content by utilizing the given command:
Step 9: Check Git Status
Check the git status of the current branch to see the changes, tracked and untracked files:
In the below output, it can be seen that the file we have created in the “alpha” branch is copied to the “dev” branch:
Step 10: Add File to Git Staging Area
Now, add the file to the Git staging area through the given command:
Step 11: Update Git Repository
Use the “git commit” command to save all the added changes and update the repository:
Step 12: Check Git Status
Now, check the Git status to verify the changes:
The below output indicates that all changes have been saved and there is nothing to commit:
Step 8: Verify Added Changes
Verify the added files by viewing the list of content in the “dev” branch:
In the below-provided output, it can be seen that we got the changes from another branch:
We have efficiently explained the method of getting changes to form another branch.
Conclusion
To get files from another branch, first, create a file in a local branch and do not add it to the Git Index. Next, switch to another branch. Then, view the status of the branch. After that, add the untracked file of the previous branch to the new Git branch index and commit changes. Check the Git status and verify the files in the new branch. This article demonstrated how to get changes from another branch.