0

I made a mistake starting a new repo by pushing to remote master. I want to take those changes and put them in a branch and have none of them in master.

I tried following this Move all files from master to another branch in Git but don't know if it was really doing what I wanted since I also want to remove files from remote master. I want to be able to make a pull request.

Thanks

Harry
  • 35
  • 4
  • `I made a mistake starting a new branch`, this is not mistake but most common workflow with git. It is not clear what is your problem, please [edit] your question and provide more solid details. Copy paste what those commands print: `git status` `git branch` `git diff --stat master `. – Marek R Aug 03 '22 at 10:15
  • @MarekR Sorry, meant to say new repo not branch – Harry Aug 03 '22 at 10:19

2 Answers2

0

If you want to move commits from one branch to another, you are looking to cherry-pick, then you can drop the changes on master.

On master do:

git log --pretty=oneline --graph --decorate --abbrev-commit 

This will show all the commits on master. Find the one(s) you want, and copy the commit hash (it's the 8 chars value you'll see). Then checkout your branch and do:

git cherry-pick <commit_hash>

Finally, checkout master and do:

git rebase -i HEAD~<number_of_commits_to_drop>

I'm assuming here your commits on master are the latest. If they are not, you'll need to increase the <number_of_commits_to_drop> so the ones you want show. A window will open showing something like pick xxxxxxxx commit_message. Simply replace pick with d for the commits you want to drop, save and exit.

Your new branch now has the new commit(s) and the master branch doesn't.

fpdo
  • 86
  • 5
0

Creating a new local branch you can use this command:

$ git checkout -b <branch-name>

Out of context, if I understood your question properly, I would look into git stash, git pop, git revert, git cherry-pick commands.

https://git-scm.com/doc, also this is the place I would start, about learning git.

user1
  • 19
  • 4