简体   繁体   中英

Git move from one branch to another branch

I am new to Git and didn't find much help for the task I want to do.

I have a master branch https://my.company.com/appid/WebApp/tree/DEV from which I have taken the code and did lots of modifications. Unfortunately I cannot push my commit back to it and I have to commit to an entirely different branch https://my.company.com/13179069/WebApp/tree/DEV , where 13179069 is my id at my workplace. I tried to do it with GitHub desktop tool but to no avail.

I assume you cannot commit to the master branch means you do not have the permission to do so.

(i) In that case, you should first create a fork of the repository on https://my.comapny.com/appid/WebApp/tree/DEV . The fork you have created will have the URL https://my.comapny.com/13179069/WebApp/tree/DEV assuming 13179069 is your GitHub ID too.

Here is the doc on how to create a fork, and also create a local clone of your fork.

(ii) Next, you can create a branch on your fork with git checkout -b <new_branch_name> and then make all the changes you need to make. Maybe just copy paste the changed files from your current unbranched unforked repo.

(iii) Once you have done that, do a git status to check if all the required files are changed, then git add <file_names> to stage some of them, or git add . if all the changed files belong to one commit.

(iv) Now, do a git commit with your message and/or signature. If you have more commits, stage them again by doing (iii)

(v) Now, you should set the upstream and origin. Do that by following this .

Once you have done all this, just push your branch to your remote origin and by git push -f origin <new_branch_name> and then create a Pull Request to the original repository by following this .

The person with merge rights will then merge your commit to the master of the original repository. Hope this helps, let me know if you need any clarification.

You're confusing a few things. https://my.company.com/appid/WebApp/tree/DEV is not a "branch". It's the url of a remote repository. The tree/DEV looks extraneous. This repository can contain multiple branches and can be exposed via. HTTP (which is what it looks like is being done here). From the url, it looks like the branch on which development is done is called DEV . This is confusing since you say that you have something on master . Can you double check this using the git branch command? Let me restate your situation to make sure I got it right.

  1. You've cloned from a repository (let's call it origin ).
  2. Made some changes on a branch X (I think it's DEV but you say it's master ).
  3. Cannot push the updated X back to origin
  4. Want to push X to a different repository https://my.company.com/appid/WebApp/tree/DEV (let's call this mine ).

If this is what you need, your problem is easily solved. All you need to do it to add mine as a new remote using

  git remote add mine https://my.company.com/13179069/WebApp/tree/DEV

Now you can push your branch to mine like so

  git push mine X

This is pushing the same branch onto a different remote .

If OTOH, you really want to move commits from one branch to another as you've stated in your question heading (but which I doubt), you'll need to do the following.

Let's assume that the original repository looked like so when you cloned from origin. C4 is the commit that's currently master .

C1 - C2 - C3 - C4 (master)

Now you made some extra commits and your local repository looks like this

C1 - C2 - C3 - C4 - C5 - C6 - C7(master)

Oops! These 3 extra commits should have gone onto another branch. First thing you do, create a branch dev at this point.

git branch dev

Now, it looks like this

C1 - C2 - C3 - C4 - C5 - C6 - C7(master, dev)

Now, we'll make master move back to the origin location (C4).

git reset --hard master C4

Now, it looks like this

C1 - C2 - C3 - C4(master) - C5 - C6 - C7(dev)

Now, you can add your own repository as a new remote (say mine ) and then push dev to it like I've mentioned above.

The answer might be a bit configuration (github instance) specific depending on your workflow, but the way it sounds, in your case you should probably fork https://my.company.com/appid/WebApp/tree/DEV into your own repo. Clone that, do your work, commit, push your changes into your repo in github, and then make a pull request for these changes from your repo to the "main" repo.

A side note. There seems to be a bit of confusion regarding the nomenclature in the original question. git is distributed SCM. You commit to your local clone (and that very likely works regardless what your remote is unless extra hooks have been put in place). I pull commits from and push commits into the remote repo, which is likely the step you cannot perform as your workflow expects you to go through an extra hop.

It seems that you do not have permission to commit in master branch. But as you have written that you have done lots of modification in master branch and now you want to commit it in another branch.

You do not have to write your code again. Follow below steps which may help you.

  1. Stash your current changes with below command.

    git stash

    Above command will store your modified code in temp memory.

  2. checkout to your new branch with below command.

    git checkout

  3. Apply your changes to your current branch with below command.

    git stash apply

    Above command will fetch your modified code from temp memory and apply to your current branch.

Note : Make sure you have proper permission to commit your code in your branch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM