Git is free and open-source that has given access to programmers and developers to add their projects in any programming language. GitHub and GitLab are platforms where gits can be created where programmer and developers add their projects to work on it and also let the team remotely work on it. The projects can also be set to public to help other programmers, non-programmers, and students to take the reference. When working on a git, you may experience many errors, and “Updates were rejected because the tip of your current branch is behind” is one of those.
No need to panic when you encounter an error as we are here to help you resolve the error with top-notch tips and solutions. Have a look at how you get the error warning
How the error pops up
When you are working on a git, this error shows up
Pushing to [email protected]:some-project/some-repo.git
To [email protected]:some-project/some-repo.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:some-project/some-repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
How To Fix the Error “Updates were rejected because the tip of your current branch is behind”
The cause of the error is the change on a remote branch without having it locally. While git is known for the local and remote branches concept. The local version of the git repository that existed in the branch is known as the local branch. On the other hand, a remote location is one that existed on a remote branch. The issue is about the remote branch that is not declared locally.
Check out the step-by-step process to solve the error
Step-by-Step Process
The program code that results in the error message:
$ git status
On branch main
Your branch and 'origin/main' have diverged,
and have 3 and 12 different commits each, respectively.#<<<<<<
(use "git pull" to merge the remote branch into yours)
You get the error as it requires merging the local remote.
First, backspace the git:
$ rm -rf .git/
After this, the next step is to push it again:
$ echo "# docker-compose_jupyternotebook" >> README.md
$ git init
Initialized empty Git repository in /Users/joohyunyoon/Documents/Workspace/.git/
$ git status
On branch master
$ git add .
$ git commit -m "edit files"
[master (root-commit) 8aef74f] edit files
6 files changed, 505 insertions(+)
...
$ git remote add origin [email protected]:mellamonaranja/docker-compose_jupyternotebook.git
$ git status
On branch master
nothing to commit, working tree clean
$ git push origin master
Counting objects: 9, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 4.59 KiB | 4.59 MiB/s, done.
Total 9 (delta 0), reused 0 (delta 0)
To github.com:mellamonaranja/docker-compose_jupyternotebook.git
* [new branch] master -> master
It helps you resolve the error message.
Conclusion
We discussed the step-by-step process to remove the error “Updates were rejected because the tip of your current branch is behind”. You can now resolve the error and continue with your git.
I hope you find it helpful!
In case of any query, feel free to drop a message in below comment field!