0

I want to commit a file to master with git, I have followed this link: How to push certain files to origin/master in git?

Now when I push my changes, get error. How can I fix it?

$ git checkout master
M       project/Build.scala
M       project/plugins.sbt
A       XX/src/main/resources/a.properties
M       XX/src/main/resources/project.conf
D       XX/src/main/scala/X.scala
M       XX/src/test/scala/Y.scala
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 8 commits.
  (use "git push" to publish your local commits)

$ git push
Password for 'https://CCC@ibgit.com:8443':
To https://CCC@ibgit.com:8443/scm/xxx.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://CCC@ibgit.com:8443/scm/xxx.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git pull
error: Cannot pull with rebase: You have unstaged changes.
error: Additionally, your index contains uncommitted changes.
Community
  • 1
  • 1
user1615666
  • 3,151
  • 7
  • 26
  • 23
  • 2
    The hint says it all. You checked out your LOCAL master branch and tried to push to origin/master without pulling the remote changes in first. Make sure you `git fetch` and `git pull`, merging changes, before you push your changes. Before you pull, you need to commit the latest changes. – Lupinity Labs Nov 21 '16 at 01:51
  • I run this command in Local master branch: git stash save --keep-index and then git stash drop . then I tried to git pull, got this error: error: Cannot pull with rebase: Your index contains uncommitted changes. how to fix it ? – user1615666 Nov 21 '16 at 02:03
  • AFAIK, git stash save --keep-index will leave all already added changes intact, that means they will be stashed (and dropped afterwards), but they will remain intact in the index and thus still need to be committed before pulling. So to fix it, commit them, stash them without --keep-index or revert/discard them. – Lupinity Labs Nov 21 '16 at 02:14
  • Why is this tagged GitHub if you're ...not...using...GitHub? – MD XF Nov 21 '16 at 04:48

1 Answers1

3

Seems like you have unstaged/uncommitted changes. You need to first commit your changes using the below command:

git commit -m 'Your commit message'

Then pull the latest code from your remote, using the following command:

git pull origin master

Commit your merged code again using the first command. After all these steps, try to push the code again using:

git push origin master

Hope this helps. Cheers.

Syed Shoaib Abidi
  • 2,356
  • 17
  • 19