Before you begin, if you are uncomfortable with a command line, you can do all the following steps using SourceTree, GitExtensions, GitHub Desktop, or your favorite tool.
To solve the issue, you might have two scenarios:
1. Fix only remote repository branch which is behind commit
Example: Both branches are on the remote side
ahead === Master branch
behind === Develop branch
Solution:
Clone the repository to the local workspace: this will give you the Master branch, which is ahead with commit
git clone repositoryUrl
Create a branch with Develop name and checkout to that branch locally
git checkout -b DevelopBranchName // this command creates and checkout the branch
Pull from the remote Develop branch. Conflict might occur. if so, fix the
conflict and commit the changes.
git pull origin DevelopBranchName
Merge the local Develop branch with the remote Develop branch
git merge origin develop
Push the merged branch to the remote Develop branch
git push origin develop
2. Local Master branch is behind the remote Master branch
This means every locally created branch is behind.
Before preceding, you have to commit or stash all the changes you made on the branch behind commits.
Solution:
Checkout your local Master branch
git checkout master
Pull from remote Master branch
git pull origin master
Now your local Master is in sync with the remote branch. As a result of the above command, other local branches branched from the previous local Master branch are not in sync. To fix that:
Checkout the branch that is behind your local Master branch
git checkout BranchNameBehindCommit
Merge with the local Master branch
git merge master // Now your branch is in sync with the local Master branch
If this branch is on the remote repository, you have to push your changes.
You need to rebase your dev branch with master.
You got the above message because after checking out dev branch from master, the master branch got new commit and has moved ahead.
You need to get those new commits to your dev branch.
Steps:
git checkout master
git pull #this will update your local master
git checkout yourDevBranch
git rebase master
there can be some merge conflicts which you have to resolve.
Imagine the scenario in which main or master is your primary branch, and also you have another branch as dev, and your dev branch is N commits behind main or master, and there's nothing about this living in your local machine.
How to proceed...
Firstly you have to make a clone from the Github repository to your machine, however, at first, your local git doesn't have to know about your secondary branch status, only from the main or master, so Git gets only your remote label branch updated, and there are no commits yet in your local.
So, what you need is at least 1 commit, 1 push to main or master, and 1 pull back to you local.
Do NOT push anything for your dev branch else you would have 1 commit ahead and N commits behind main or master (so, conflicts)...
So, you'll need an auxiliary branch to send a Pull Request to your remote main or master.
Without further ado. How to proceed in this scenario:
Ater cloning the repository...
git checkout -b aux-branch -> it'll create and checkout the branch
Now, you have to make at least one change in order to record a first commit in you local Git, suposing you haven't anything to implement at the moment... Take the README.md file to change a letter, a space, a blank line more...
git status -> you'll see your README file was modified git add README.md -> to add it to staged area, ready to the commit git commit -m "modified readme file" git push -u origin aux-branch -> it should generate a link that'll lead you to your repository to open and accept the ordered Pull Request.
I use accepting that with Rebase and Merge (the last option in green button when dropdown), after the accepting GitHub will ask you if you want to delete aux-branch... yes you want, so delete, 'cause your main or master now has this newest feature brought by aux-branch.
Now back to your local (Git), proceed this following way:
git checkout main or master git branch -D aux-branch -> to Delete in your local too\ git remote update --prune-> it makes both fetch and update to your remote. git pull -> to receive the newest updates from remote GHub. git checkout dev git rebase dev main or master -> (rebase or merge) git push -u origin dev -> climbing the updates to your remote branch
Now go to your GitHub reload the page, drop down to your dev branch you'll see your dev branch is the same as your main/master branch...
If the message says that you are 'n' commits behind master, then you can use the 'git sync' option in tortoise git. Where you will be prompted to select from the branch (target) that you want to pull your changes from. Hit 'Pull'. This will pull all the changes from remote (master/any target branch) into your development branch. After pulling, hit 'Push' to push all the synced changes from your local development branch to remote development branch on git.
In intelliJ you can do following simple steps -
checkout your branch and pull latest commits from main/branch name
Merge those changes in your branch . - > vcs -> git -> merge changes (origin/main)