0

I recently pushed my first repository in the GitHub account from Linux Mint Terminal. It was quite straightforward. I can see my repository with all my files is there. I am also able to clone it successfully. All set.

Now I just want to update my README.md file or any other file and update this single file to GitHub account using the command line tool. Somehow, I am not able to do that. I dont want to login my github account and edit the file there, because this way my local and repository wont be same (unless I again pull it back to my local machine).

I tried this:

  1. Edited the file.
  2. git add .
  3. git commit -m "adding a README File to the repository"
  4. git push origin master

In the 4th step, I get the below error:

Username for 'https://github.com': xxx
Password for 'https://xxx@github.com': 
To https://github.com/xxx/MyTest.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/xxx/MyTest.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.

What am I doing wrong? There is any thing missing or the whole thing is wrong itself. I did not get a straight answer to this. Please suggest.

Gaurav Bhaskar
  • 195
  • 2
  • 4
  • 18
  • I don't see anything wrong with the steps you did take, only with the steps you didn't take. You need to `git pull` to bring in the latest information from the remote `master` branch. – Tim Biegeleisen May 06 '18 at 14:14

2 Answers2

5

The error message basically tells you what you need to do, which is to git pull. Your problem is that since you have pulled, someone else has committed work to the remote master branch which is not part of your local master branch. Try pulling to remedy this:

git pull origin master

You might get merge conflicts from this, in which case you would have to resolve them, then commit manually. After this, you should be able to push with no issues:

git push origin master
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    Thank You. So for each change I make in any of my file, I will have to do this exercise. – Gaurav Bhaskar May 06 '18 at 14:29
  • Not necessarily. If, after you have done a pull, commit, and push, you do more work _and_ no else pushes something to the remote `master` branch, then you might not have to `git pull` again. You may think of `git pull` as the means by which you sync your local branch with everything happening to the remote branch. – Tim Biegeleisen May 06 '18 at 14:31
  • I tried to make changes again and I had to follow the below steps: 1. Edited the file. 2. git add . 3. git commit -m "3rd Commit" 4. git pull origin master 5. git push origin master – Gaurav Bhaskar May 06 '18 at 14:48
1

Your question seems to match this one

git commit should be followed with a commit message and not a file (git commit -m "your message")

Godlove Damian
  • 137
  • 1
  • 4