Git

How to Get Changes From Another Branch?

While working on Git, developers interact through branches. Developers are permitted to create multiple branches against each module. However, sometimes, they need to switch from one branch to another during development. In this situation, there is a chance of losing the project data. To avoid this, copy the data of one Git branch to another or add the changes of one branch to another.

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:

$ cd "C:\Git\test_1"

Step 3: Create a File
To create a file, use the “touch” command and add the file name:

$ touch testFile.txt

Step 4: Verify Created File
View the list of files to verify if the file is created or not:

$ ls

Step 5: Check Git status
To view the current repository status, run the below-given command:

$ git status

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:

$ git branch

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:

$ git checkout dev

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:

$ ls

Step 9: Check Git Status
Check the git status of the current branch to see the changes, tracked and untracked files:

$ git status

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:

$ git add testFile.txt

Step 11: Update Git Repository
Use the “git commit” command to save all the added changes and update the repository:

$ commit -m "testFile is added"

Step 12: Check Git Status
Now, check the Git status to verify the changes:

$ git status

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:

$ ls

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.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.