I created a new Rails app and pushed the code to Github directly from master (first commit in the repository). However I made a mistake, I didn't want to commit this new Rails app directly from master, but instead create a new branch from master and push the new Rails app from this new branch.
Therefore, I'd like to:
Delete the commit from master in Github(remote), so master is EMPTY
create a new branch from master and add the previous commit that was in master into this new branch.
push it to Github.
Delete the commit from the master in Github(remote), so master is EMPTY
You can create an orphan branch - orphan branch is branch without any history
# Create "clean" branch
git checkout --orphan <name>
# remove all existing content if you wish
git clean -Xdf && git clean -xdf
create a new branch from master and add the previous commit that was in master into this new branch.
few options:
# Option 1 - Start your branch from the last desired commit
git checkout -b <name> <SHA-1>
# Option 2 - Set your branch to the desired commit
git reset <SHA-1> --hard
# Add the required commit on top of your branch
git cherry-pick <SHA-1>
push it to Github.
# force the update of the new branch
git push <origin> master -f
Please create a new branch from the master with the following command,
git checkout -b branch_name
After that checkout into your new branch and push it to Github.
Now go to the master branch and remove the last commit and push it to Github
You can also try the following steps with your repository:
git checkout master: make sure you are in master.
git checkout -b my-fancy-new-branch: create your new feature branch.
git checkout master: switch back to master
git reset --hard rootcommit: reset master to the state before your own commits.
optional and if you have a remote you pull from: git pull --ff (if this fails because the pull is not fast-forward, you have to reconsider rootcommit. It contains some of your work)
I tried to do this on my test repository, it appears to work.
I have taken a reference from this answer, that can help in finding other approaches too.
Related
I have two branches in BitBucket server.
1. master
2. feature
Process is:
we have to take the latest code from Master. Once our changes or script creation is done. we will push it to feature for review. If review is approved, it will be merged with master for automation purpose.
First Time,
>> git clone "hosturl"
Make some changes
>> git checkout feature
>> git status
>> git add *
>> git commit -m "test"
>> git push
It is getting pushed in to Feature branch.
From Bitbucket UI, I am trying to CreatePullRequests
It shows select source and destination.
I am selecting source as Feature and Destination as Master and entering reviewer mail id. It is working fine.
The same case for next time onwards I am getting issues
>> git checkout master
>> git pull
Make some changes
>> git checkout feature
>> git status
>> git add *
>> git commit -m "test"
>> git push
It is getting pushed in to Feature branch.
From Bitbucket UI, I am trying to CreatePullRequests
It shows select source and destination.
I am selecting source as Feature and Destination as Master and entering reviewer mail id. It is showing
`Pull request creation was canceled`.
•Please rebase your branch on to the target branch before creating a pull request: git checkout feature; git rebase master; git push -f
When pulling from master and push to feature we are getting the above error. But when clone from master and push to feature working fine as said (first time).
How to resolve this?
I see that your feature branch and master are not in sync. You need to merge the master changes into feature branch.
Try these steps:
git checkout master
git pull
<Make changes>>
git add *
git checkout feature
git commit -m "test"
git merge --no-ff origin master
git push
I have two branches in bitbucket and I want to push only some commits (which are approved by me) from one branch to another branch which is a master branch.
Can someone explain to me how I can do it in the bitbucket environment?
Thanks in advance.
you can use git cherry-pick
steps
1. git log
this will list all commits take the commit id which you wanted to move.
2. git cherry-pick <commit-id>
apply this after switching to master branch
3.then push new master to remote
git push <REMOTENAME> <BRANCHNAME>
eg. git push origin master
or
git push <REMOTENAME> <LOCALBRANCHNAME>:<REMOTEBRANCHNAME>
eg:
if my remote name is heroku and i want to push local heroku branch to heroku(remote) master barnch(heroku/master)
git push heroku heroku:maste
links
What does cherry-picking a commit with git mean?
I am using Bitbucket in my project. When I am cloning the code on my local machine with my branch URL I am getting the updated code of my branch and unable to see the changes in the other branches. How to clone the updated code from all branches in Bitbucket?
First check which branch you're on and if you have already checked out the master branch:
git branch
You should see something like, although the names will probably be different:
dev
master
* test
Switch to the master branch. If master WAS NOT listed in the previous step run the following:
git checkout -b master origin/master
If master WAS listed just run:
git checkout master
You will now be on the master branch. Now do a git pull for the latest updates.
I'm working on a new branch. Basically I've messed my code up and would like to go back to master and start fresh from a new branch.
Would $ git revert get me back to the master?
or should I be doing the following:
$ git branch -D branch-name
I want to get rid all the code and current entire branch that is checked out.
The online documentation is a bit confusing - scared to mess everything up.
To get back to the master branch simply use:
git checkout master
If you also want to blow away uncommitted changes you can follow that with:
git reset --hard
You can remove the unwanted branch using:
git branch -D branch-name
Yes, git branch -D branch-name would delete the given branch. The commits would remain in the repository until garbage-collected. Make sure that you don't have that branch checked out when you delete it. To do so, just check out master again with git checkout master, then you can run the delete command.
If you want to go back to master, just type in this:
git checkout master
Then start a new branch from there.
Try:
git checkout master
git branch -d messed-up-branch-name-goes-here
Other information may be found at the git book.
i use the git workflow as described in this blogpost.
In short: everybody develops inside his/her own branch, before merging back to master, you rebase your branch to master again to get clean history.
This works.
Now we have a submodule, and because this is an in-house plugin (Rails), we need to change this often. So most of the times i have changes both in the general branch and in the submodule branch.
What is the best way to work with submodules in the workflow as above.
I first try to push my changes to the submodule (git checkout master, git pull, git checkout branch, git rebase master, git checkout master, git merge branch).
Then, when i try to do the same for my root, i always get an error on my plugin (submodule). I have to resolve the error, before doing git rebase --continue. So if try to git mergetool i converts my folder to a file.
After the rebase has ended, i just restore the <folder_name>.orig to overwrite the file <folder_name> and all is well.
But somehow it feels there should be a better way.
In short: when working via checkout-b/rebase/merge - workflow, how do you handle the changed submodules simultaneously?
Whatever workflow you are following with submodules, there is one rule you shouldn't forget:
(From the Git tutorial)
If you want to make a change within a submodule, you should first check out a branch, make your changes, publish the change within the submodule, and then update the superproject to reference the new commit.
$ git checkout master
$ echo "adding a line again" >> a.txt
$ git commit -a -m "Updated the submodule from within the superproject."
$ git push
$ cd ..
$ git add a # There is a gotcha here. Read about it below.
$ git commit -m "Updated submodule a."
So did you commit the new state of your submodule within the parent project before attempting your rebase/merge from said parent project?