Back to [[git:main-git|git main page]]
When another user has modified a file and has pushed it before you, you will get a rejection like this when try to **push** :
git push
Username for 'https://gitlab.com': MyUserName
Password for 'https://MyUserName@gitlab.com':
warning: redirecting to https://gitlab.com/MyUserName/my-project.git/
To https://gitlab.com/MyUserName/my-project.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitlab.com/MyUserName/my-project.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
When trying to **pull**, you will also have a rejection, like this :
git pull
Username for 'https://gitlab.com': MyUserName
Password for 'https://MyUserName@gitlab.com':
warning: redirecting to https://gitlab.com/MyUserName/my-project.git/
remote: Enumerating objects: 19, done.
remote: Counting objects: 100% (19/19), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 12 (delta 9), reused 5 (delta 2), pack-reused 0
Unpacking objects: 100% (12/12), done.
From https://gitlab.com/MyUserName/my-project
12511b3..2a1f0ac master -> origin/master
Updating 12511b3..2a1f0ac
error: Your local changes to the following files would be overwritten by merge:
file_that_has_changed.txt
Please commit your changes or stash them before you merge.
Aborting
There are many ways to solve this problem. One way is to use **stash**. This command will temporarily remove our modifications.
git stash
Then we do a hard pull to update our branch, here we use the **master** branch :
git reset --hard origin/master
Then we add our modifications by bringing back (popping) the stashed data :
git stash pop
Finally we can push our changes using the standard way, with add commit and push :
git add file_that_has_changed.txt
git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD ..." to unstage)
modified: file_that_has_changed.txt
git commit -m "add my changes"
[master f70fbb4] add my changes
1 file changed, 2 insertions(+)
git push
Username for 'https://gitlab.com': MyUserName
Password for 'https://MyUserName@gitlab.com':
warning: redirecting to https://gitlab.com/MyUserName/my-project.git/
Counting objects: 5, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 432 bytes | 432.00 KiB/s, done.
Total 5 (delta 4), reused 0 (delta 0)
To https://gitlab.com/MyUserName/sal-2020
2a1f0ac..f70fbb4 master -> master