github default branch is main but not master - github

I created a new repo in GitHub.
I init the git git init locally and
git remote add origin https://github.com/username/repo_name.git
then created and added few files locally git add then commit git commit -m "commit msg"
when I pushed it to my GitHub repo, it's creating a new branch master but not default, however, the default is the main branch [strange, never saw this branch before].
Now I have main (default) and master branches which are unexpected.
And, also didn't let me merge the pushed file.
How do I simply have master branch as default and ignore main branch? if not what's the way to let create pull request on main and merge the files?

Simply change the master as the default branch.
Simple as that.

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 do I switch my fork to a different Github branch?

I made fork a branch of a Github repository. I made changes to my fork.
A new branch was added to the repository. I want to merge the changes from the new branch to my fork, preserving my changes to my fork.
I don't own the original repository.
How do I do this?
#add the remote to your fork.
git remote add upstream https:/github.com/whoever/whatever.git
#update your personal fork while keeping changes (swap master with the branch you want).
git pull upstream master --rebase --autostash
This should add the remote repository to your git repo (your fork) and then you tell it to fetch the changes from that branch and add your own changes on top.

Bitbucket: How can i create new branch without copying Master branch

I just need to create a new branch without copying the master branch which is already in repo.
Is there any way to create a separate branch which has separate code in the same repo?
Since Git 2.23, you would use the new (still experimental) command git switch.
In your case: git switch --orphan newBranch
Create a new orphan branch, named <new-branch>.
All tracked files are removed.
That branch won't have any common file/history with master.
(Before 2.23, git checkout --orphan <new-branch>, but using checkout is no longer recommended, since it deals both with files and branches)

Sync my Pull request with someone else commit some changes - Github

I sent a PR and someone else made some changes so it has 3 more commits and now I need to sync that PR with my local to continue working on some changes. How could I do to sync my local with the PR. Thanks
A pull request is basically just a designated branch on your fork of the repository. Update that branch (by rebasing its commits on top of those new commits) and the pull request will automatically update to show these changes.
First, you need access to the additional commits in your own fork of the project. To do so, you can add the upstream repository as an additional remote and fetch the commits from there:
git remote add <new-remote-name> <upstream-url.git>
git fetch <new-remote-name> <upstream-branch>
Then you can rebase your changes on top of the upstream changes:
git checkout <your-pr-branch>
git rebase <new-remote-name>/<upstream-branch>
git push <origin> <your-pr-branch>
You need to rebase your branch to reflect all the merged work of others in your PR. Here is how you can do this:
Let's say upstream is the name of the remote repository which you have forked, and origin is the name of your forked repository.
git pull --rebase upstream master
git push origin master
There can be merge conflicts sometime during rebasing. In that case, check the files in which conflicts are there using:
git status
The name of the files which are in red color are the files having conflicts. Resolve the conflict manually. After resolving, add the files using:
git add <filename> or //for each conflicted file
git add --all //to add all files at once
Once again check the status using the git status command. If all the files are shown in green that means they are resolved and you can now continue your rebase. Run the below command for continuing your rebase:
git rebase --continue
Once done, simply push the changes to your forked repo using:
git push -f origin master
Note: In case, you have not set the remote url of your upstream and origin, add them using following commands:
git remote add upstream <upstream-url>
git remote add origin <origin-url>

Git branch and local changes

Lets say I do a git checkout -b my_new_branch. Making some changes to my local files and after this I add all files with git add. and commit it and push. After this i realize that my branch is messed up and I want to delete it.
So Im going back to my master with git checkout master and delete the branch with git branch -D my_new_branch.
Will all local changes be reversed?
Once you checkout the master branch, all of the changes that you made on my_new_branch will no longer be in your working directory. They will still be on your branch and on the server.
Once you delete my_new_branch, the ref to that branch will be deleted. The commits will still exist in your local repository until a garbage collection is run. The commits and branch will also still exist on the remote server.