44

Here is my situation: I have a production branch, a dev branch and branches for features. While I was working on a feature I has to do a hotfix on the dev branch. Now I to rebase the feature branch I am currently working on to avoid future merging conflicts. When I used to use IDEs from JetBrain I would do a git rebase and it would do the trick. The only command I found in VSCode is Git: Sync(rebase) but this does not do anything and does not rebase. All I get is this message:

This action will push and pull commit to and from 'origin/Current_feature'

Anyone have experience with this?

1
  • 1
    It sounds like “Git: Sync (rebase)” does git pull --rebase, as a companion to “Git: Sync”, which might do a plain git pull. git pull --rebase rebases new commits on the branch instead of merging them. Commented Jul 18, 2018 at 18:47

7 Answers 7

28

I just tried, it works!! enter image description here

PS: I try to find the approach to setting the default "Sync" action with --rebase paramter. I find the PR of Added config option to sync+Rebase from statusbar, but it haven't landed.

Finally find the solution, git config --global pull.rebase true and it works!!

3
  • 6
    You can now Sync+Rebase since September2018 release code.visualstudio.com/updates/… Commented Nov 7, 2018 at 9:14
  • 3
    This wrapper for git pull --rebase provides a way to rebase while pulling (that is, to fetch and then rebase on the remote branch), but not a way to rebase HEAD on an arbitrary commit. Commented Jan 29, 2019 at 3:00
  • 7
    This only rebases your local commits on top of new pulled-in commits in the same branch. It doesn't allow rebasing on another branch (such as git rebase origin/master), which I think is what OP was asking. I'm still not sure if vscode has a non-terminal way to rebase on another branch. Commented Feb 21, 2020 at 11:55
24

I don't think Visual Studio Code has Git rebase functionality built-in. If you want to do your rebasing in Visual Studio Code instead with the git command-line tool or with a Git GUI, you can install the GitLens extension for VS Code.

GitLens’s README indicates that GitLens supports rebasing. It says that when viewing branches, the context menu for each branch includes these commands:

  • Rebase (Interactive) Branch (via Terminal)
  • Rebase (Interactive) Branch to Remote (via Terminal)

And when viewing the commits in one branch, the context menu for each commit includes this command:

  • Rebase to Commit (via Terminal) (when available)
1
23

You could run the command directly from your terminal with: git rebase branch or git rebase -i branch

You will have to configure your gitconfig to use vscode for the interactive rebase.

Something like:

[core]
  editor = code --wait

Or by setting your envar to GIT_EDITOR=code\ --wait

As soon as you want to do something a bit out of the ordinary, using the command line yield better results.

1
  • 2
    This fixed my problem! I had only "edit = code" and when doing git rebase -i origin/master vscode did the rebase automatically ... Commented Jan 23, 2021 at 17:47
13

Since VS Code 1.51 (October 2020) a command named "Git: Rebase branch..." (git.rebase) has been added: https://code.visualstudio.com/updates/v1_51#_git-rebase-command

2
  • 2
    This is the correct answer! From your feature branch: hit F1 -> Git: Rebase branch... -> select the develop branch (main branch). Done.
    – Nomnom
    Commented Apr 21, 2023 at 13:15
  • Don't forget after you complete the rebase on local feature branch, you need to do a force update to the remote: git push --force
    – gb96
    Commented Aug 9 at 1:07
9

Looks like this VS Code PR introduced a new git.rebaseWhenSync option: https://github.com/microsoft/vscode/pull/52527

I tested it and it looks like it works as expected.

4

You can set the git config to automatically open VSCode for rebases like this:

git config --global core.editor code

Then you can simply rebase with this command:

git rebase -i name-of-branch

enter image description here

Note 1: In order to have the nice formatting like shown on the screenshot, you'll need to install GitLens.

Note 2: If you are using VSCodium, you should configure git to use it instead:

git config --global core.editor codium
3

Seems that in the recent version of VSCode you can do Pull+Rebase like this:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.