1

I have cloned a remote branch A from github, and checked out a local branch feature/test from it

For example -

git clone A
git checkout -b feature/test

Now i started working on feature/test and did some changes in it and commited but not pushed. Now the remote branch A has been updated with latest commits from someone, so i want to update my local branch feature/test and take these latest changes from remote A. How can i do this with out losing my local changes.

Note - my local changes from branch feature/test has already been commited but not pushed.

Derrick
  • 3,669
  • 5
  • 35
  • 50
  • Use merge or rebase? I don't follow the question entirely. If you've already _committed_ your work, then a simple merge or rebase on the parent branch should suffice for bringing in the latest changes, unless I'm missing something. – Tim Biegeleisen Jun 10 '20 at 05:38
  • @TimBiegeleisen so i want keep the local changes and continue working on my local branch after taking update from remote – Derrick Jun 10 '20 at 05:46
  • You _can't_ really do this, since the only ways to update are via a merge or rebase, which will be bringing in new commits to your local branch. Yes, you can keep your work, but other (new) commits would likely also be there, somewhere. – Tim Biegeleisen Jun 10 '20 at 05:47
  • Does this answer your question? [Make an existing Git branch track a remote branch?](https://stackoverflow.com/questions/520650/make-an-existing-git-branch-track-a-remote-branch) – Greg Burghardt Jun 10 '20 at 11:12

1 Answers1

2

The problem is a very common one, what you can do is that you can change the base of your branch. You can follow the steps below

  • git fetch origin (This will get all the updates which have happen on the remote)
  • git rebase origin/A (here I am assuming you are on your current branch in which you have committed your work).

Here you can face a conflict (maybe) so you have to resolve that too before you can successfully get those changes.

  • Both fetch and rebase will be done while being on my local branch which i want to update right ? – Derrick Jun 10 '20 at 06:18
  • by any chance will this affect other local branches ? – Derrick Jun 10 '20 at 06:22
  • fetch does not effect any of your branch it just gets the latest state of the origin, without effecting any of you local branches. The rebase will only effect the one branch you are rebasing (in our case feature/test). After rebasing this branch will now have the changes which were added on the remote version of this branch and your commit will be added in the last. – DHAWAL JAIN Jun 10 '20 at 08:07
  • Ok. After fetch I did merge instead of rebase, so the latest commit on remote is merged with the local branch now and commited those changes again. Is what I did correct ? Because I think anything can be used out of rebase and merge right ? – Derrick Jun 10 '20 at 12:05
  • 1
    Yeah both are fine, just two different ways with slight variation, your job will be accomplished with any. – DHAWAL JAIN Jun 10 '20 at 18:45