1
  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.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
ceth
  • 44,198
  • 62
  • 180
  • 289
  • 1
    possible duplicate of [Change the current branch to master in git](http://stackoverflow.com/questions/2763006/change-the-current-branch-to-master-in-git) – Chris Maes Nov 19 '14 at 15:09
  • @ChrisMaes That question is almost identical, except for the fact that in this question the OP is on a detached head, which could cause some of the answers to the linked question to not work for the OP here. The basic question is pretty much the same though. Hmm... tough call. – Ajedi32 Nov 20 '14 at 14:21

2 Answers2

2

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.

Community
  • 1
  • 1
Ajedi32
  • 45,670
  • 22
  • 127
  • 172
  • 1
    This will create problems for any collaborators since you are **force** updating a branch. – Justin Howard Nov 19 '14 at 15:05
  • Technically speaking commit `C` won't be *permanently* deleted, you just lose the reference on the commit. It still exists in the repo and can easily be found using a command like `git reflog` or the plumbing command `git fsck`. – Sascha Wolf Nov 20 '14 at 05:59
  • @Zeeker Good point, there aren't many git commands that *permanently* delete anything. If you've committed it at any point, it's probably recoverable. – Ajedi32 Nov 20 '14 at 14:13
2

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.

Justin Howard
  • 5,504
  • 1
  • 21
  • 48
  • Is it possible to keep commit 'C' as another branch ('experiment', for example) for the future investigation ? – ceth Nov 19 '14 at 15:12
  • If you use `revert`, commit C will always be in your master branch. So you can still do `git show C` to see the patch. All revert does is create a commit with the opposite effect. – Justin Howard Nov 19 '14 at 15:13