-1

I have a question regarding git rebase.

I have my main branch - call it dev. I have created a branch from dev - call it myTest. i have created another branch from myTest - call it myTest/myTestPart1

----------+------------------- dev
            \
              --------+-------- myTest
                        \
                          ------- myTest/myTestPart1

When someone push something to dev I do

git checkout dev
git pull

then I do

git checkout myTest
git rebase dev
git push -f

then I do

git checkout myTest/myTestPart1
git rebase myTest
git push -f

Today another developer was working with me and he said I needed to do

git fetch origin
git rebase origin/dev

What is the difference between what i was doing and what he said to me?

Thank you

Janos Vinceller
  • 1,208
  • 11
  • 25
ZombiePie
  • 299
  • 1
  • 4
  • 14
  • Maybe this question/answer can better clarify https://stackoverflow.com/questions/3357122/what-is-the-difference-between-git-pull-and-git-fetch-git-rebase – sensorario Oct 08 '21 at 22:31
  • Since you have sub-branches (`myTestPart1` that is based off of `myTest`), if you rebase `myTest` you'll need to do a fancy rebase to keep `myTestPart1` up to date. See the second half of [this answer](https://stackoverflow.com/a/69303822/184546) for one way to accomplish that. – TTT Oct 09 '21 at 18:59

2 Answers2

1

One difference is that he uses sentences that start with a capital letter and end with a period, and you don't. :)

What he's saying is that you're wasting a lot of effort and time and Internet bandwidth. You don't need to checkout dev and pull it in order to rebase onto it. And since you don't need to checkout dev, you don't need to swith to myTest — because you are already on it.

The way to rebase myTest onto latest version of dev is to stay on mytest and say

git fetch origin
git rebase origin/dev

You have to admit that's a lot shorter than what you're doing.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • So i can do git fetch origin in my branch myTest and then git rebase prigin/dev should i do the same with myTest/myTest1Part1 or from the first step itself my branch will be already updated. The dev made git fetch origin in the branch myTest/myTestPart1 thank you – ZombiePie Oct 08 '21 at 22:21
  • OK, that is a really good question. No, if you rebase `myTest` that has no effect on `myTestPart1` that branches from it. Let me ask you this: has `myTest` grown since you branched from it, or does `myTestPart1` carry on from the end of `myTest`? – matt Oct 08 '21 at 22:33
  • It grows as i have other branches linked to myTest. Once the linked branches are approved they are merged in myTest. I create myTest/myTestPart2 for example so when approved it will be merged in myTest. – ZombiePie Oct 08 '21 at 22:47
  • Well, having rebased MyTest, you'd need to rebase the relevant part of MyTestPart1 onto that. But what you're doing is not a sensible way to work. – matt Oct 08 '21 at 22:53
  • Actually its like a feature. All the tests concerning the festure is in one folder. There is only dev-myTest-myTest/myTestPart1 no more than that. – ZombiePie Oct 08 '21 at 22:57
  • After doing git fetch origin and git rebase origin/dev on myTest. I should do git fetch myTest and git rebase origin/myTest on myTest/myTestPart1? – ZombiePie Oct 08 '21 at 22:58
  • I'm with you in spirit, but feature branches need to be independent of one another. – matt Oct 08 '21 at 23:00
  • However, I've done my best to answer the question you actually asked, which is what your friend meant and why it's different from what you're doing. – matt Oct 09 '21 at 00:24
0

The fetch command is mandatory if you want to be sure you are working with updated branches. Without this, ... you rebase into a local history of dev that can be a little bit different from yours.

From atlassian documentation:

In review, git fetch is a primary command used to download contents from a remote repository. git fetch is used in conjunction with git remote , git branch , git checkout , and git reset to update a local repository to the state of a remote.

If you rebase into your local (and not updated) dev you could simply lost some informations and some colleagues updates.

sensorario
  • 20,262
  • 30
  • 97
  • 159
  • When i do git pull isn't all the content updated? – ZombiePie Oct 08 '21 at 22:24
  • I think @ZombiePie this answer can better explain what happens: https://stackoverflow.com/questions/3357122/what-is-the-difference-between-git-pull-and-git-fetch-git-rebase – sensorario Oct 08 '21 at 22:30