1

I use what is called "Git Flow" which means there is a 'develop' branch where all work results of all team members are collected, while each developer creates a 'feature' branch and issues a pull request to get the feature branch in to 'develop' in the end, while the feature branch is deleted.

If I now do a 'git pull' followed by a 'git checkout develop', I get back to the last commit of my local develop branch, which is older, and only another 'git pull' updates my local files to what is now in 'develop' on the remote. What I hate is this behavior of getting OLD files back when going back from my feature branch to 'develop', just to be updated to the recent files (I already had a minute or so ago) after issuing the second 'git pull'.

So, question is, is there any way to avoid this back and forth shuffling of files in my working directory, if all I want is to switch to 'develop', to the latest commit on the remote, immediately?

K.Isbruch
  • 145
  • 2
  • 8
  • Along the same idea of matt's answer, if your feature branch is merged into `develop` and deleted, and you don't keep a local copy of `develop` anymore, you can do `git fetch`, and then `git switch --detach origin/develop` and then `git branch -D my-finished-feature-branch`. Now you're up to date and ready to create a new feature branch without having a local copy of `develop` which is always out of date. – TTT Mar 29 '23 at 22:06

2 Answers2

2

git pull updates (merges or rebases) the currently checked out branch. To update non-current branch(es) you need git fetch. So it's either

git checkout develop
git pull [origin [develop]]

or

git fetch origin develop:develop
git checkout develop

You can create (git or shell) aliases, shell functions or scripts to do the repetitive task.

If the current branch is not develop you can run git pull origin develop:develop. This runs git fetch origin develop:develop and then merges (or rebases) develop into the current branch. But git checkout develop will be needed afterward anyway.

phd
  • 82,685
  • 13
  • 120
  • 165
2

You should not have a local develop branch at all. Delete it. Delete it now. It is just making your life a living hell, as your question rightly suggests.

All you need is the automatically generated remote tracking branch origin/develop. And you already have that, because it is (wait for it) automatically generated.

So now:

  • To bring all your remote tracking branches up to date, including origin/develop, just say git fetch (no matter what branch you are on).

  • If you need to incorporate the latest develop changes into your current feature branch, then after saying git fetch, say git merge origin/develop. Or, if you have not pushed your current feature branch and you want to pretend that it emerges from the most recent state of develop, say git rebase origin/develop (but that is riskier).

(And never say git pull again.)

For a more complete explanation of how I do git flow, see my essay here: https://stackoverflow.com/a/72201388/341994

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • A very interesting and helpful thought, to never use a local branch I cannot commit to, like my 'develop' branch. That I am using git IN THE WRONG WAY is clearly the right answer, which makes my question how I can achieve what I wanted to do originally, obsolete. I recommend everybody to follow the link of your "complete explanation", for understanding your answer here. Thanks a lot! PS: only "issue" I have with git detached is that git cli doesn't show me the branch name, like origin/develop, but just the commit id. SO it's a bit hard to see which remote branch I am currently "in". – K.Isbruch Mar 31 '23 at 10:57