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!
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With