sábado, 23 de novembro de 2024

My git rebase annotations: Using another editor, nano, for rebasing

⚠️Be careful using any of those commands. Only use them if you know what you are doing.

These steps and commands are here for my reference.

Git reset

If just want to clear a series of commits without losing the work that was done on files:

git reset --mixed [Commit-Id]

The commits after CommitId will be removed on your local branch and the files that were on these commits will return to stage area, so now you can create a new commit wich includes the files:

git commit -m "Write your commit message here"

And then send this new history to the server and replace the older one with:

git push --force 

Git rebase

If want to have more customization, then git rebase can be useful.

Basically, what was missing for git rebase to work as I was expecting was: 

  • Configure the rebase editor to nano
  • Configure Visual Studio to enable the git force

Before starting, I needed to configured my Visual Studio and my git CLI:

  • On Visual Studio, it is possible to enable the git force:

TOOLS > Options

Git Global Settings

Enable push --force-with-lease


It is possible to use the nano editor on git, by:

  • Setting the default editor for Git:

Pick one:

  • Set core.editor in Git config:

 git config --global core.editor "nano"

  • Set the GIT_EDITOR environment variable to nano:

Linux:

export GIT_EDITOR=nano

Powershell:

$env:GIT_EDITOR="nano"

  • Then when running git rebase --interactive, for example:

git rebase -i HEAD~3

git rebase -i 57ec1fce4d46664d5800afc31ca5fa664a5f729a

  • You get a screen like this:


Notes:
On the rebase interactive editor, top is the oldest commit, going to the newer commit on bottom.
It starts displaying the next commit after the commit Id that you supplied on command line.
(If we compare with the other window, for example, we can see how this ordering happens.)
Important: Avoid rebase change/rename/delete commit from other branch, for the commit id will change and when merged there will be two commits of this same thing.

  • Do the appropriate editing;
  • Save the file and exit the editor. Then check with:
git log --oneline
  • To to revisit the changes before applying, use:
git rebase --edit-todo
  • at this stage it is possible to cancel this rebase and return to previous state with:
git rebase --abort
  • If everything is correct then can conclude the rebase with:
git rebase --continue
  •  At this stage, the git timeline on the branch may look like this:

  • If everything is correct, it is possible to send the modified history timeline and commits and override the older one on remote by using:
git push --force

Note that the --force had to be used to rewrite the history on the remote/server. If I dont use --force, it ends up getting duplicate commits for the same things due to the all commit ids mentioned inside the interactive session changed locally by rebase.

  • Then the git timeline on the branch will become like this:


An example about the commit ids change:
git rebase -i dad9ccff3752390be71890587f237667a5fa08b8
I reword 24630e51 to T2 - Adding first File on T2 and rewording...
Then see what happened:

Before:

Then, after git push --force:


The commit Id changed to d24750b1, and also all other commits following this one got new commit ids.


Notes:
Some useful commands:

git status
git log --oneline
git branch [branchname]

git switch [branchname] 

git commit -m "Write your commit message here"

git push 

To switch to another branch:

git branch [branchname]

Rebase:
git rebase -i [Commit-Id]
git rebase --edit-todo
git rebase --abort
git rebase --continue
git push --force

On Windows I got a message regarding .
I solved it by running:

git credential-manager configure

Other options I did not use yet:

git rebase --autosquash --autostash -i 59649c515ffdbe765dca7441a95395a7e6043fa1~

git rebase --autosquash --autostash --interactive HEAD~3

 

See also:

    git config --global core.editor "code --wait"

. 🎉

Nenhum comentário :