⚠️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:
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:
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
Then see what happened:
Before:
git statusgit log --onelinegit branch [branchname]
git switch [branchname]
git commit -m "Write your commit message here"
git push
git branch [branchname]
Rebase:
git rebase --abort
I solved it by running:
git credential-manager configure
git rebase --autosquash --autostash -i 59649c515ffdbe765dca7441a95395a7e6043fa1~git rebase --autosquash --autostash --interactive HEAD~3
Nenhum comentário :
Postar um comentário