You mention that you think you are "overwriting changes". While that is what it looks like on the surface, it isn't actually true below the surface in the repository. The repository is a Write Once Read Many (WORM) type storage, so all your previous writings are still there, waiting to be re-used and brought back.
Things take weeks to be removed by the garbage collector (gc) and perhaps an explicit --prune command. (also search for 'git lost & found' for ways of discovering apparently abandoned work) The gitk visualiser tool can be useful for investigating what you have stored. There is also the recent blog https://dev.to/lydiahallie/cs-visualized-useful-git-commands-37p1 which shows some nice graphics of how the repo storage extends. The most common problem, as a beginner, is forgetting to create a fresh branch (which is simply a pointer to the 'WORM' storage), so that your new work isn't confused with what was on the 'master' branch. The second thing is then to suddenly realise that "remotes" aren't really remote, but actually a local copy of what was on the true remote server last time you looked. This means that you should "never" use 'master' because really what you wanted to refer to was the branch called 'origin/master' (other remote names are possible, not just 'origin) when you created your new branch (i.e. work on top of what was at the remote!!). This is why the 'pull' got confused. It fetch the remote server (to origin//master) and then found that you'd done some extra work on the side having been asked to merge as a fact forward.. tut tut.. (pull = fetch + merge) Usually, after your mistake of extra work on the wrong branch (such as master): try `git switch -c my_new_branch master` (This assumes you are 'clean', if not stash your changes) then later you can `git switch -C master origin/master` (this puts you back on master, back at the point it should have been) finally `git switch my_new_branch` (back to your work, un-stashing any changes you needed to hide away while doing the shuffle) [Note the small c and big C in the two commands, check the manual `git help switch` to see why the second command needed the big C)] Hope that helps. On Monday, April 27, 2020 at 3:48:22 AM UTC+1, SJW wrote: > > I keep trying to learn and use git but I keep screwing up my work - > overwriting changes and creating duplicate efforts. > > I made some changes today, but was on the wrong branch - when trying to > change branch I screwed up > I made the changes again and then tried to push to master and I get this: > > $ git push origin master > To https://gitlab.com/SJWGitLab/absee.net.au.git > ! [rejected] master -> master (non-fast-forward) > error: failed to push some refs to ' > https://gitlab.com/SJWGitLab/absee.net.au.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. > > > It says do a git pull BUT, my understanding is that doing a pull will > overwrite my code with the origin and delete my recent changes. > > I did a git diff but it gave me nothing... I dont want the origin to be > the master - my local is the master... > > I'm having a real hard time with git and dont understand how it is so > popular - it has added an extra layer of complexity and confusion on top of > managing code that is creating so much rework and recoding... What am I > missing? > -- You received this message because you are subscribed to the Google Groups "Git for human beings" group. To unsubscribe from this group and stop receiving emails from it, send an email to git-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/git-users/7a484341-257c-48b6-9039-75f74e3605e1%40googlegroups.com.