GIT - Commit to another branch

Asked

Viewed 4,605 times

5

I am working on a project that has two branchs (master and dev).

I started the implementation of a new Feature in the system but, halfway through, I realized I was making the changes in the master, instead of the dev.

There’s a way that I can finish the code and commit directly to the dev branch and leave the master as it was before I started coding?

Or the way it’s gonna be:

Copy everything I’ve done; To delete the project; Download again from the repository; Go to dev branch; paste everything I did.

Thanks to those who can help.

  • Have you committed any of these new master changes??

  • @Viana not yet

  • 1

    Obviously you can finish coding and create a new branch with these changes: Ex: git checkout -b Feature/2 ... ai you 'comitta' inside. Then just merge with dev. It’s an output.

  • If you haven’t added or committed or pushed the master yet, it’s very simple to resolve. Do what I’m doing. You can even do it before you finish coding. It will create a new branch and migrate automatically. But watch out for the Feature number. Try to put something that doesn’t exist yet.

  • @So, I created a new branch here, with another name and everything but my master is still with the changes, I can just undo the changes?

2 answers

9


Finish what you have to finish and commit normally. Then make sure you are in master using git checkout master and use git log to view all the commits you’ve made. Check which ones you want to change, and identify them at the beginning of your hash, for example:

commit 53d51100b6bbcd7c27ae19d8ba5765f76afb9184 (HEAD -> master)

is an identifiable commit at the beginning 53d5.

Then go to the branch with

git checkout dev

And use the cherry-pick to bring the commits you made in the master:

git cherry-pick hash1 hash2 hash3... 

Where the hashes are the identifiers I mentioned above.

After this step, the branch should contain the commits you did in master, and the master as well.

To remove commits from the master, and leave them in their initial state, identify the first commit you leave the way you want and run:

git checkout master
git reset --hard hash

Where hash is the commit identifier you want to leave the master in.

  • It’s expensive, I was flushed here and I sent a reset-hard in the master by the visual studio and I ended up losing the things I had done, now I’ll have to redo everything again. but thanks for the help.

-1

If you haven’t committed your changes yet, you can use git stash.

First you give a git stash, then everything you’ve changed will be saved in memory, and then you switch to the correct branch and a git stash apply that will apply changes to the branch you’re in.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.