Undo git stash pop that results in merge conflict
2022-05-05
Question I began making changes to my codebase, not realizing I was on an old topic branch. To transfer them, I wanted to stash them and then apply them to a new branch off of master. I used git stash pop to transfer work-in-progress changes to this new branch, forgetting that I hadn't pulled new changes into master before creating the new branch. This resulted in a bunch of merge conflicts and loss of a clean stash of my changes (since I used pop).…
Git diff -w ignore whitespace only at start & end of lines
2022-05-04
Question I love to use git diff -w to ignore whitespace differences. But, I just noticed that it ignores even whitespace differences in the middle of lines. How could I only ignore whitespace differences that come at the start (^) or end ($) of lines? Answer For end of line use: git diff --ignore-space-at-eol Instead of what are you using currently: git diff -w (--ignore-all-space) For start of line... you are out of luck if you want a built in solution.…
How do I git rebase the first commit?
2022-05-04
Question I used git init to create a fresh repo, then made three commits. Now I want to rebase to go back and amend my first commit, but if I do git rebase -i HEAD~3 it shows error - fatal: invalid upstream 'HEAD~3'! If I try the same with HEAD~2 then it kinda works but only lets me rearrange the last two commits. How do I refer to the 'commit before there were any commits' or go back and insert an empty commit?…
How to stash only unstaged changes in Git?
2022-05-04
Question I would like to use this workflow: Stage some changes. Save the unstaged changes to the stash. Do some stuff with the things in stage (build, test, etc.). Commit. Restore the unstaged changes. Is there a way to do step 2? Example: git init echo one >file git add file git commit echo two >>file git add file echo three >>file git stash push test git commit git stash pop Answer git stash push has an option --keep-index that does exactly what you need, so run:…
What would I use git-worktree for?
2022-05-04
Question I read Github's post on git-worktree. They write: Suppose you're working in a Git repository on a branch called feature, when a user reports a high-urgency bug in master. First you create a linked working tree with a new branch, hotfix, checked out relative to master […] You can fix the bug, push hotfix, and create a pull request. When I'm working on a branch called feature and some high-urgency bug in master is reported, I usually stash away whatever I'm working on and create a new branch.…
How to modify existing, unpushed commit messages?
2022-05-03
Question This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. </div> I wrote the wrong thing in a commit message. How can I change the message? The commit has not been pushed yet. Answer Amending the most recent commit message git commit --amend will open your editor, allowing you to change the commit message of the most recent commit.…
Putting Git hooks into a repository
2022-05-03
Question Is it considered to be a bad practice - to put .git/hooks into the projects repository (using symlinks, for example). If yes, what is the best way to deliver same hooks to different Git users? Answer I generally agree with Scy, with a couple of additional suggestions, enough that it's worth a separate answer. First, you should write a script which creates the appropriate symlinks, especially if these hooks are about enforcing policy or creating useful notifications.…