0

I opened my previous changes of my files that was pushed to online git repo and started editing them.

When I hit push it told me to pull and work before pushing to online repo again.
But I already made changes to the files and it is a pain to do it again from start.

So if I pull the online repo files to my local repo which is already edited, will the changes get lost and will I have to start again?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
sravanTG
  • 574
  • 5
  • 9

2 Answers2

1

The (fast-forward) pull itself may fail on a conflict or the branches being simply out of sync (which will create a merge commit). What you can do is issuing a rebase together with the pull:

git pull --rebase

which will prioritize the changes in the remote repo and apply your changes on top of those.

Or you can divide the work by

  • creating a new branch from your local one (e.g. as a backup): git checkout -b backup
  • re-create the original branch: git branch -D master; git checkout origin/master; git checkout -b master
  • then rebase your backup on top of it: git checkout backup; git rebase master

Or play with cherry-pick to pull only specific commits.

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
1

I would recommend:

  • first, add and commit your work in progress: no matter what you do next, you will always be able to find back that commit.
  • then modify your configuration (if you are using Git 2.27+):
    git config --global pull.rebase true
    git config --global rebase.autoStash true
    
  • finally, a simple git pull is enough.

No need for git pull --rebase: it will be done for you.

That will:

  • pull new commits from remote rpeository
  • replay your local commits (your work in progress) on top of it.

you might have conflicts to resolve, but that is the all point: resolve those conflicts locally, before pushing back your work to the remote repository.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I pulled the project accidently to my local repo but changes were present and nothing was lost. but i will use the methods u guys told from now on. ... thankyou for you help.... – sravanTG Jul 21 '21 at 08:32