-1

I plan to have two branches.

dev - contains code with addional logging output
production - contains only code, without logging output

I just created a new commit in dev:

$x = getFoo();
$this->logger->info("Value of x: '$x'");
...

Now I changed to the production branch and try to fetch the local branch dev, without merging it, so that I can remove the logging debug output and create a commit with the same commit message as in dev branch.

Attempt:

So I tried to fetch the local dev branch, but I get:

$ git fetch dev
fatal: 'dev' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

How can I solve it?

Black
  • 18,150
  • 39
  • 158
  • 271

2 Answers2

3

git fetch is the wrong command for this:

  • You made a commit, locally, on one of your branch names.
  • Then you switched to some other branch name. This other name selects a different commit: not the commit you just made, where you added some code, including some debug code. The commit you have selected now includes neither the new code nor the new debug code.
  • You now want to take the stuff that you already committed—i.e., that you already have—and use that to do some additional work on the files you have extracted from this other branch and its different commit, without making a new commit.

The git fetch command is about getting commits from some other repository. You don't need to do that! You already have the commit you want, in your own repository, right now.

The command that's closest to the right one for this is git cherry-pick. This command isn't really a solution, though: it's just a tool, like git diff or git apply. Its default is to do a sort of diff-and-apply-and-commit, all in one. You want a sort of diff-and-apply without the commit. Fortunately, that's easy to get: just add -n to the cherry-pick command:

git checkout production
git cherry-pick -n dev

Unfortunately, this has the side effect of also doing the equivalent of:

git add <files>

where the files are the ones modified by the diff-and-apply. That's not a big problem, really, but it means that you'll want to clean up the debug prints and re-run the git adds.

torek
  • 448,244
  • 59
  • 642
  • 775
  • I tried it, it works good if the `dev` branch is ahead by only one commit, but if it is ahead by multiple commits, then it just loads the latest commit from the dev branch. Is there a solution for this too? – Black Nov 11 '20 at 12:29
  • In this case, you have the option of cherry-picking multiple commits (if that's what you'd like as your result, i.e., multiple commits) or of using `git merge --squash` or equivalent (if that's what you would like), or of using `git merge --no-commit` (if that's what you'd like), or using any of Git's other various tools. There are many tools. Decide what *result* you want first, then pick the tools that will get you there. – torek Nov 12 '20 at 01:56
  • I tried `git merge dev --no-commit` but there are many conflicts. I think the best solution is my patch workaround in this case. – Black Nov 12 '20 at 12:08
-1

if dev branch is one commit ahead

If the branch is only one commit ahead, then I prefer the cherry-pick solution from @torek. This has the advantage that it will only include the changes since the last commit without all logging code.

if dev branch is multiple commits ahead:

git diff production dev > patch
git checkout production
git apply patch
Black
  • 18,150
  • 39
  • 158
  • 271