1

I have three branches, the main branch, the development branch and the new_feature branch.

The problem is I accidently set my first commit (initial commit)on the development branch.

Screenshot of my problem: enter image description here

Full overview of my branches: enter image description here Question: How do I change it to the main branch?

Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
  • 1
    This answer is better than I could give: http://stackoverflow.com/questions/1628563/move-the-most-recent-commits-to-a-new-branch-with-git – tehp Dec 08 '16 at 03:39

4 Answers4

0

You can merge your develop branch with your master branch or use the cherry-pick command from your master branch for retrieving your initial commit.

0
git push -u origin development//(push it up to development)
git checkout main //checkout local main branch
git pull origin development //Pull latest from remote development branch into main
git add -A
git commit -m "get latest from development"
git push -u origin //push up to main
williamsandonz
  • 15,864
  • 23
  • 100
  • 186
0

You can rename your development branch.

$ git checkout development                 # checkout development
$ git checkout -b development-backup       # backup development branch
$ git branch -D master                     # delete your current local master 
$ git branch -m development master         # rename development -> master
$ git checkout master                      # checkout master
$ git push -f origin master                # force push, update remote master
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
  • I didn't see any reason to: 1)rename the `dev` branch to `master`, he still has remote `dev` branch to track of and very importantly his `master` has different hashs with `dev` 2) **force** update to the remote master, nothing is messed up with the `refs/remotes/origin/master` he just need a `reset` or `revert` action and then keep in sync with remote branch. `cherry-pick` or `rebase master` -> `checkout master` -> `merge dev` would be my choice. – Allen Dec 08 '16 at 04:21
0

As you haven’t merge remote branches (origin/development and origin/new_feature) into local repo. So there are two situations

Don’t need remote changes. Follow these steps:

  1. Create a new “initial commit”. git checkout master, git checkout --orphan master1 and then git commit
  2. Rebase development branch on it. git checkout development and git rebase master1 (if there has conflict file, you use to use git add filename and git rebase --continue).
  3. Delete master and new_feature branch. git branch -D master and git branch -D new_feature
  4. Rename branch. git branch -m master1 master, git checkout master and git branch new_feature
  5. Force push to remote. git push -f --all

Need remote changes. Follow below steps:

  1. Merge remote changes to local. git checkout development, git merge origin/development, git checkout new_feature, git merge origin/new_feature
  2. Create a new “intial commit”. git checkout master, git checkout --orphan master1 and then git commit
  3. Rebase development branch on it. git checkout developmen and git rebase master1
  4. Rebase new_feature branch on it. git checkout new_feature and git rebase master1
  5. Rename master branch. git branch -D master and git branch -m master1 master
  6. Force push to remote. git push -f --all
Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • Thanks for this! but may I ask what the difference is between a normal `branch` and a `remote branch`? Why is that when I try to create a new branch, a `remote` one is also created? – Matthew Francis Dec 11 '16 at 04:36
  • Yes, of cause. Local branch is just "work for" remote one, the remote branch is truly the place you do the version control. And local branch can track remote branch (you can check the connection relationship by `git branch -a`). when you create a new local branch and want to push it to remote, it will create the same branch in remote by default. The `git push` is just equally to `git push origin newbranch:newbranch`. Assume if you want to push local new branch to remote master branch, you can use `git push origin newbranch:master` – Marina Liu Dec 12 '16 at 09:09
  • Thanks! another thing, you mentioned about `remote changes`, what does `remote changes` mean ? and when do you want to have `remote changes`? it looks like it's related to the merging of remote branches and local branches together. – Matthew Francis Dec 13 '16 at 05:06
  • Remote changes means the commit IDs for `origin/new_feature` and `origin/development`. Just as your shared picture, we can found `origin/new_feature` and `origin/development` are not point the same commit id with `new_feature` and `development` branches in local. This means you only fetched from remote but not merged the changes in local. So before you re-organize your branches order, you should decide whether the remote changes to merge in local or not. – Marina Liu Dec 13 '16 at 05:22