Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT: Adding Local Changes to Non-Current Branch

Tags:

git

git-branch

It usually happens to me that I make some local changes, only to discover that I made it on the wrong branch, so I need to switch branch before committing. The problem is that I cannot switch branch when there are local changes. Is there anyway to do that?

Obviously, I can copy the updated files, switch branch, then copy them back, but this doesn't really seem clever!

like image 524
Rafid Avatar asked Jan 07 '11 09:01

Rafid


People also ask

How do I commit a local change to another branch?

Create a new feature branch, say feature, and then switch to that branch. Implement the feature and commit it to our local repository. Push to the feature branch to the remote repository and create a pull request. After other teammate's review, the new change can be merged into the master or release branch.

How do I push changes to a new branch?

Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch. Simply use a git push origin command on subsequent pushes of the new branch to the remote repo.

How do I bring changes to a remote branch to a local branch?

In order to fetch these changes from your remote, or in other words, download the changes to your local branch, you will perform a Git pull. Under the covers, a Git pull is actually a Git fetch followed by a Git merge. Git pull is just a shortcut to perform both of these actions in one step.


1 Answers

You can switch branches while you have local modifications unless your local changes conflict with the difference between the two branches. In this case you can use the -m or --merge option to checkout to perform the checkout anyway and perform a merge betwee changes and the changes caused by switching branches.

git checkout -m other-branch
like image 165
CB Bailey Avatar answered Oct 26 '22 02:10

CB Bailey