0

I've only changed 2 files and they are the only ones I want to push to Github. I've added the 2 files I wanted and committed the changes with comments. However, when I push i get the following:

git push
To github.com:TinsleyBridgeGroup/TBL-bending-presses.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:TinsleyBridgeGroup/TBL-bending-presses.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.

Based on the above i git pulled and received the following error:

git pull
error: Your local changes to the following files would be overwritten by merge:
        TBL165-238.json
        TBL165-279.json
Please commit your changes or stash them before you merge.
Aborting

How can I push the 2 files I've changed "read_pressure.py" and "TBL165-238.json". A bit of background, the script is used on multiple IOT devices. The json file is settings that are machine specific and the read_pressures.py is generic and run on multiple computers. Git status command is listed below.

git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 3 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   .vscode/launch.json
        modified:   .vscode/settings.json
        modified:   TBL165-238.json
        modified:   TBL165-279.json
        modified:   read_pressure.py

no changes added to commit (use "git add" and/or "git commit -a")
resolver101
  • 2,155
  • 11
  • 41
  • 53
  • 2
    You push commits, not files; and a commit always contains a snapshot of _the whole repository_. However, your actual problem is about having _uncommitted_ changes; you'll find plenty of existing questions discussing this by [searching for that error message](https://stackoverflow.com/search?q=%5Bgit%5D+%22Your+local+changes+to+the+following+files+would+be+overwritten%22). – IMSoP Jun 08 '21 at 12:14
  • 1
    Possible duplicates: [How do I ignore an error on 'git pull' about my local changes would be overwritten by merge?](https://stackoverflow.com/questions/14318234/how-do-i-ignore-an-error-on-git-pull-about-my-local-changes-would-be-overwritt) or [error: Your local changes to the following files would be overwritten by checkout](https://stackoverflow.com/questions/22424142/error-your-local-changes-to-the-following-files-would-be-overwritten-by-checkou) – IMSoP Jun 08 '21 at 12:15

2 Answers2

1

Please try with the below steps.

  1. git stash
  2. git pull
  3. git stash apply (resolve conflict if there is any)
  4. git add .
  5. git commit -m 'message'
  6. git push
Praveen kalal
  • 2,148
  • 4
  • 19
  • 33
  • Thank you that did the trick. At stage 2, I still find the changes difficult to read but i guess that comes with practice? or is there tools that are better than using the command line. – resolver101 Jun 08 '21 at 13:10
  • Just so I understand what I did, the "git stash" stores to a separate (stashed) copy out of the way. "Git pull" transfers the changes from remote (ie.githum in my case) to local, "git stash apply merges" the stashed copy with the freshly pulled version. 4 to 6 is understood by me. – resolver101 Jun 08 '21 at 13:15
  • @resolver101, it seems you have two interconnected problems: your local main (assume it is main) is not up to date, and you have non-committed changes in your local working area. Basically you are trying to push something that is not the same as your local version, and that in turn is out of date with the latest version on github. There are times when using stash so you can merge or rebase your local version makes sense, so the instructions on how to recover are useful, but you should not normally be trying to push ever with uncommitted local changes. – johnfo Jun 10 '21 at 07:21
0

You are trying to push files without getting the current changes, so Git prevents you to change the future by using the past. That's why you need to pull before.

If you don't want to pull files that would erase yours then either you need to commit them to add them to the modifications to make, or, you want to pull by keeping your local changes :

as mentioned in git pull keeping local changes, use this :

git stash
git pull
git stash pop

Then, you can

git push