ceth
ceth

Reputation: 45325

Rename the current branch to master

  1. I have git repository with commits A-B-C (from earlier to newer) - on the server and on my machine. It is master branch.
  2. I decided that last commit was not as good as I thinked before. So I have made

    git checkout B 
    
  3. I have made some changes and commits. So now I have:

    A-B-C  (master branch synced with server)
      \
       D-E-F ('detached from 6ebd863' branch)
    

Now I want to work 'detached from 6ebd863' branch and rename it to 'master'. What the best way to do it.

Upvotes: 1

Views: 233

Answers (2)

Ajedi32
Ajedi32

Reputation: 48428

Move your existing master branch to the new commit:

git checkout -B master HEAD

Note that this command will forcibly update the master branch, removing commit C from the repository's history. If you haven't yet pushed commit C to a remote repository which collaborators may have pulled from (i.e. commit C is only in your local repository), this probably isn't an issue. If others may already have pulled a copy of commit C though, might want to go with a less destructive option such as the one suggested in justinhoward's answer.

Upvotes: 2

Justin Howard
Justin Howard

Reputation: 5643

Since master is already pushed the server with commit C, you should revert C first, then merge in D,E, and F. This will preserve your history in the master branch.

git checkout -b tmp-fixes
git checkout master
git revert C
git merge tmp-fixes

You could force update the branch as suggested by Ajedi32, but that would rewrite history which can cause major problems when you are working with collaborators.

Upvotes: 2

Related Questions