1

I started working on a new project and from the beginning I pushed all my commits to master. But now I think I should've used another branch for development and stored my code there and only have the production build in my master.

Is there any way I can move all my files from master to a new branch?

Shamim Fahad
  • 132
  • 2
  • 12
  • 3
    If you create a new branch while on master (e.g. with `git checkout -b new-name`) they'll all *already be there*. – jonrsharpe Jun 25 '20 at 07:03
  • 1
    Create a new branch at `master` then [Reset master back to where it should be](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit) – Liam Jun 25 '20 at 07:04
  • Does this answer your question? [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit) – Liam Jun 25 '20 at 07:05
  • What's your goal for the repo now? Do you just want to use a different branch for future work, or do you want to revert your master branch to be the production code and undo/migrate those commits to a different branch? Also, how many people are working in this codebase? – Brian Jun 25 '20 at 07:06
  • in there, it only undo a certain commit but I need to move everything. – Shamim Fahad Jun 25 '20 at 07:08
  • i'd like to migrate my commits to a different branch – Shamim Fahad Jun 25 '20 at 07:11
  • In GIT it's unhelpful to think of branches as tangible things, a branch is just a pointer on the graph. Your commits already exist in the graph, so your not moving anything. You simply need to adjust your pointers (branches) so that they point to the correct point on the graph – Liam Jun 25 '20 at 11:34

1 Answers1

5

Yes.

Create a branch on the current commit using git branch <whatever>. Then, run git log, and identify which commit is the latest "production" version (probably where you started working). git checkout that commit, and then git branch -f master to make the local master branch point there, and git push -f origin master to update the origin. If you work collaboratively with other people on this repository make sure to update them - force pushing is a destructive action and might cause issues, see this SO question.

Shay Nehmad
  • 1,103
  • 1
  • 12
  • 25
  • 2
    Definitely worth highlighting the last sentence **force pushing is a destructive action and might cause issues.** – Liam Jun 25 '20 at 07:06
  • 1
    Thanks @Liam, I added a link that explains some more about the dangers of force pushing – Shay Nehmad Jun 25 '20 at 11:29