by MauranKilom · 3y ago · 0 comments
How do you undo a merge that you didn't mean to do/did wrongly?
git reset --hard <last commit before merge>
Have some cosmetic fixups on your local branch that really should go into main (or a separate branch) first before merging a bigger feature? git stash
git checkout main
git stash apply
By thinking about branches as pointers, the commit graph existing independently, and stashes just being temporary commits, I feel I'm working much more directly with the underlying abstraction. Yes, git has commands for specific combinations of actions, but for an occasional user it's harder to remember every such command and which arguments and flags to pass in which order. It's either "look through documentation until you find graph diagrams illustrating what will happen for this order of arguments and flags" or "use the primitives 'move branch pointer', 'commit to branch', 'hold these changes for a second' for obtaining the commit tree you actually want. Knowing that the reflog exists also makes this insane-sounding working mode pretty non-scary. And yes, some operations (e.g. cherry-pick) you just need to do the "real" way.(My git stash obsession is most likely just damage from years of using Perforce, which doesn't have a modified/staged distinction. The only way to commit only part of a changed file is via the equivalent of stash -> [restore half the file] -> commit -> stash pop.)
Prepares to be crucified...
Nothing wrong with this at all. Only people who don't understand and/or are scared of git don't like it.
You could also use cherry-pick to "donate" commits to other branches, instead of stash, of course. Magit has some great extra abstractions for this.
It will take decades before git checkout will actually get replaced by git switch/restore in all the git books, tutorials and search results. Most normal users will keep learning and using git checkout in the meanwhile.
I understand why they can't actually deprecate existing commands. The git command is used in far too many existing shell scripts across thousands of companies.
I would argue that this is actually a fundamental deficiency of the "unix way" of doing things, where the same command is meant to be used both by human beings and in automated workflows. Automated workflows require backwards compatibility. Humans need easy to use interfaces that don't allow them to easily shoot themselves in the foot. The same tool cannot serve both needs.
>git status
On branch feature/redacted
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: redacted.py
no changes added to commit (use "git add" and/or "git commit -a")
>git checkout . Updated 1 path from the index
>git status On branch feature/redacted
nothing to commit, working tree cleanYou can checkout files and directories from completely unrelated commits and even unrelated repos!
I have used this to checkout specific useful files from other projects. It's a little nicer than just copying them in, because you can keep the repo tracking branch updated and keep checking out updates, and you can easily compare your file to theirs, and see what changes they have made to the file.
My preference is to use temporary branches and cherry-picking instead of stashing; I mostly use a gui* to work with git so it is easy to select the two or three commits to cherry-picking or see visually if an interactive rebase would work.
Your strategy of seeing things as they are is a useful general purpose life skill.
Your friends are unreasonable, unless you collaborate with them on the same branches and rewrite them after you shared them.
I tried using git worktree for a while when working on multiple branches, but it’s a pain to use… Stashing is easier.
I usually used `switch` for this:
# Check out the previous commit
git switch -d HEAD~
# Overwrite the branch
git switch -C <current branch>What would be great is a simple way to have build/lint/etc. tools look at the staging area instead of the workspace.
If you rewrite history on master… well… completely unreasonable.