I am pretty new to git. I wanted to create a project and I'm using IntelliJ to clone my repository. Unfortunately, IntelliJ has its own branch being created (called master). Now I want to submit my changes to main, not to master.
Therefore, I was trying the following:
Commit and push my changes to master in order to make sure that my changes won't get lost.
Select main and do "Rebase master onto origin/main".
Select main and do "checkout".
However, after checkout my changes are reset and the folders are empty. Did I forget something. Thanks for your help.
I explained it above.
IntelliJ has nothing to do with the name master
for the default branch when creating a new repository. This is a Git configuration. By default, Git uses the name specified in the global configuration init.defaultBranch
. If no name is specified, then master
is used. See the section "Your default branch name" of the official documentation for more details.
You can check your global configurations with:
#check your global configurations
git config --global --list
You can change the name for the default branch with:
git config --global init.defaultBranch main
Like so, every new repository on your machine will have main
as its default branch.
Getting back to your problem, If I understood your situation correctly, your repository was created with a master
branch, and at some point a main
branch was created from it. You've now made some changes on master
(commit C), and you would like to have those changes on your main
branch instead.
A -- B ---- C master
\
D -- E main
In that case, you could rebase main
on top of master
with:
#selecting the main branch
git checkout main
#relocating the main branch with master's HEAD commit as its base
git rebase master
producing the following situation:
A -- B -- C master
\
D' -- E' main
If you want to perform the same operation via IntelliJ's interface instead of the terminal, you need to select the option Checkout and Rebase onto 'master'
.
master
branch to the main
branch and reset the master
branch to its previous commit.# select the main branch
git checkout main
# re-writing the HEAD commit of the master branch on top of main
git cherry-pick master
# selecting the master branch
git checkout master
# resetting the master branch to its previous commit
git reset --hard HEAD~
master
branch haven't been committed yet. First, stash the changes from the master
branch, then checkout the main
branch and apply the stashed changes on it. Finally, commit the changes on the main
branch.# while the master branch is checked out
# stashing the uncommitted changes (untracked files included)
git stash push -u
# selecting the main branch
git checkout main
# applying the stashed changes to the main branch
git stash pop
# committing the changes on the main branch
git add <file1>, ..., <fileN>
git commit -m "my commit message"
If in the future you find yourself in the same predicament, where your default branch is called master
and you want it to be called main
, you can rename a branch with git branch -m
.
git branch -m master main