How to move an entire branch to the main branch - github

I have the following branches :
I wish to make my client-rework branch the master branch. I dont want to merge it, I want to entirely replace the master branch with the newest i created. I also dont want the master branch remove, ideally it would be renamed to something like master-old or something. Is this possible or am i asking something dumb here.

you can set your client-rework to be the default branch in setting, tab branches. like in my case below, i set develop as the default branch

First thing master branch is by default protected. You have to remove that from settings, it depends which git flavor you are using. and you should not delete because that is how industry practices work. you may try git checkout master or some other branch

Assuming you are using Github you have to change the default branch first.
After that you need to rename the local client-rework branch to master:
$ git branch -m master master-old
$ git branch -m client-rework master
Now push the branches to origin remote:
$ git push origin HEAD

Related

how to move master branch to main branch on github

I have a master branch and a main branch in git-hub. This was not intentional I realized that I have been pushing with master so everything went to master branch. However git-hub is now using main instead of master as first position. How can I move my master branch into main branch?
There are multipe ways to achieve this, but with pure git it can be done as follows:
#Change to the main branch
git checkout main
#Rebase the main branch on top of the current master. In easy words, go to the last point the two branches have in common, then add all the changes master did in the meanwhile and then add the changes done to main. If main is empty this is equivalent to
# git checkout master; git branch -D main; git checkout -b main
#which deletes the current main and then copies master over to main
git rebase master
# push back to GitHub (--force, because rabse does not add a commit, but changes the status, so push will be rejected without it)
git push --force
You can delete or rename a branch through the github web interface by visiting
https://github.com/YourName/YourRepo/branches
At time of writing you can access that page from the front page of your repository through the branches link:
And you can click the writing implement buttons to rename their associated branch:
It is worth noting that deleting and renaming branches through github is just as potentially destructive as doing it with CLI git or a GUI git client. You can of course re-push branches from your local clone, but you should make sure that clone is up to date before proceeding if you feel unsure.
Create a PR from master to main in the GitHub Webinterface.
If you want to do it local:
git checkout main
git merge master
git push
Set local master to track remote main:
git branch --set-upstream-to origin/main master
For first pull it will show error refusing to merge unrelated histories so you can pull with git pull origin main --allow-unrelated-histories
Push it into the main branch in GitHub.

How to keep a GitHub fork up to date without a merge commit or using CLI?

The normal GitHub flow to contribute to a repo is to create a fork of the upstream, clone a local copy where you make changes, then push back up to your fork and then create a PR to have your changes merged into upstream.
But if upstream changes after that, how do you update your fork without creating a merge commit (and also without using the git CLI)?
I already know how to do this in a way that will create a merge commit or which depend on the git command line interface. This question is specifically about using the GitHub.com website or GitHub Desktop application only (no CLI).
Since this is a very common workflow it seems like there should be some simple way to do it using the GitHub GUI.
To reiterate: any answers that use the CLI or create a merge commit (e.g. this way) will not be answering this question since I'm explicitly looking for a non-CLI solution.
without a merge commit or using CLI?
Not directly with GitHub web UI alone, since it would involve rebasing your PR branch on top of upstream/master
So in short: no.
But in less short... maybe, if you really want to try it.
Rebasing through GitHub web UI is actually possible, since Sept. 2016, ...
if you are the maintainer of the original repo, wanting to integrate a PR branch
if none of the replayed commit introduces a conflict
(This differs from GitHub Desktop, which, since June 5th 2019 does support rebasing. But that is a frontend to Git CLI, like other tools provide. For example GitKraken and interactive rebase)
So a convoluted workaround would be:
to fetch, then push upstream/master to the master branch of your own fork (a CLI operation, but more on that below)
change the base branch of your current PR to master (so a PR within the same repository: your own fork), provided you haven't pushed to master.
Meaning: master in your fork represents the updated upstream/master, with upstream being the original repository that you have forked.
Since you are the owner of that repository (your fork), GitHub can then show you if you can rebase said branch to the base branch of the PR (master), but only if there is no conflict.
finally, change the base branch again, to <originalRepo>/master (which is the intended target of your PR)
The very first step is typically done through command line, but... there might be a trick to do it (update upstream master in your fork) through web UI: see "Quick Tip: Sync a Fork with the Original via GitHub’s Web UI" by Bruno Skvorc
In short, it involves:
creating a new branch from your current master (which would be at upstream/master at the time you forked the original repository)
Making a PR with that new branch and <originalRepo/master>
doing a base switch before creating the PR
That is the step which artificially forces upstream/master to be refreshed
You can the create and merge it with the “Merge Pull Request” button (and “Confirm Merge” afterwards): the merge will be trivial: no merge commit.
The end result is: your own master branch (in your fork) updated with upstream/master (the master branch of the original repository)!
You can then resume the steps I describe above, and change the base of your current PR to your own (now refreshed) master branch, and see if you can rebase it!
This is feasible with GitHub Desktop since version 1.0.7 considering the following:
If the current branch does not have any commits ahead upstream (the original repo of the fork), the new commits can be pulled without creating a new merge commit
In GitHub Desktop:
Clone your repository from File > Clone Repository
Fetch origin, which will automatically fetch the upstream as well
Go to Branches by clicking on where it says Current Branch
Click on Choose a branch to merge into <branch> at the bottom
Search for upstream/<branch>, then click Merge upstream/<branch> into <branch>
Push to origin, et voilà!
Otherwise, ff the current branch has commits ahead of the fork, then of course one has to create a merge commit or rebase and force push. For rebasing which might be more preferable, do the following:
In GItHub Desktop, go to Branch from menu, then Rebase Current Branch
Search for upstream/<branch>, then click Start Rebase
Solve any conflicts that have occurred from the rebase
Force push to origin. You will get a warning for this for obvious reasons.
For avoiding force-pushing to your work when your current branch is both ahead and behind its upstream counterpart, either create a new merge commit or:
Make a new branch based with all your changes
If needed, reset the original branch to its original state (before it diverged from the original repo)
Perform the steps from the first scenario and merge your changes into your branch.
And yes, it seems that pulling via the GitHub website from the original repo without creating a pull request and merge commit is not possible at this moment.
Demo GIF for first scenario: https://imgur.com/a/8wci2yf
Some GitHub issues related to this:
Add an upstream to forked repositories
multi-remote support in Desktop
Update
Note: Non-CLI based approach that might help:
Is there a way to make GitHub Desktop rebase a branch against master?
The only key here is doing a rebase, so the above answer should help.
CLI way (which is easier and using git, so it should be more comprehensive by default)
There are some practices that you should use to avoid this.
Don't work on the master branch in your fork.
$ git clone <your fork>
$ git checkout -b feature_branch
You can work in your feature_branch and then raise a Pull Request.
Once your changes are merged in the upstream master, you can pull from upstream to your origin. Since the master on upstream will have your commits sitting neatly on top of it, there won't be a merge commit.
$ git checkout master
$ git pull upstream master
$ git push origin master
In the case, where the maintainer has diverged from the master that you have in your fork, that is, it's not linear any more, you need to pull a fresh copy of it. That should not be a problem as your changes are already in the upstream.
If the master in upstream has moved ahead while you were working on your PR, then you can rebase on you feature_branch.
$ git checkout master
$ git pull upstream master
$ git push origin master
$ git checkout feature_branch
$ git rebase master
Please refer to this document for detailed reference: Fork and pull request workflow

What is the best way to use the option 'Merge [branch name] into current branch' - SourceTree

I want to know what is the best way to use the command: Merge branch name into current branch
What i am doing (Let us suppose I want to merge master into develop):
I checkout to the master branch and pull all the recent changes.
Then I go back to the develop branch.
I right click on the master branch and click Merge branch name into current branch.
And the master branch will merge into the develop branch.
Is this correct?
Your methodology looks correct.
If you are not seeing any changes tothe branch you are merging into - then it is likely this branch is already up-to-date with the branch merged into it.
You can also look up the following references on the Merge comnmand you are using:
Git Tutorial (Beginner): Using GitLab & Source Tree

change branch into new master github

When I type "git branch" i get this:
Zachs-MacBook-Pro:stocks1 zachsmith$ git branch
77e98af109bd63630b38c1f1ca3937d43715ddf4
add_bootstrap
add_stock_model
master
stocks_download
temp
* working
working#2(backup)
Does this mean i am on "working" branch of the detached head "77e98af109bd63630b38c1f1ca3937d43715ddf4"?
i want where I am now to become the new master on github, but I am not sure how to do that without merging things. basically I would be happy to just re-write the master with where I currently am.
How can I do this?
Does this mean I am on "working" branch of the detached head "77e98af109bd63630b38c1f1ca3937d43715ddf4"?
No, it just means:
you are on the working branch nammed "working"
there is another branch named "77e98af109bd63630b38c1f1ca3937d43715ddf4" (probably some mishap in the git branch command)
basically I would be happy to just re-write the master with where I currently am.
You can rename the remote master
git branch old_master origin/master
git push origin old_master
And force push your working branch
git push --force origin working:master
You would see a similar approach in "Rename master branch for both local and remote Git repositories".
You have multiple options in order to do that, one is to delete the master branch and rename your working branch as master, but you might know you can be sentenced up to 25 years of jail and death penalty for that.
You can also rename your old master branch and rename your working branch as the new master, or (my favourite way) you can use the -s ours flag so you keep your master intact and you overwrite everything from your working branch:
git checkout working
git merge -s ours master
git checkout master
git merge working
And your master now will match to your working branch.

git hub branches

I am using github and I have the following question, I have a master branch and I created a new branch out of master by doing
Git branch {branchname"
Git checkout {branchname}.
Now I need to edit some settings file , should I edit this file from my new branch or the Master? If I edit it from the new branch, then each time I create a new branch for different projects, then I should be modifying this settings file…but at the same time if I edit it by being on master branch, then when I merge/update master branch later, then I would loose the changes. Can someone please clarify the best way to handle this? If I have to do it from Master, then how I can stash it as I never push anything from master branch.
it depends what you want. If you want the new settings to be available everywhere, you need to modify the file in master, check it in, then rebase any existing branches to get the settings.
If you only want the changes in the other branch, make them there. If you ever merge the other branch back into master, then master will then have them.