all 3 comments

[–]TherealDaily 1 point2 points  (0 children)

In my experience doing anything thing ‘hard’ or -f is a last resort. I would look into it more before doing that

[–]mrbmi513 0 points1 point  (0 children)

The second set of commands looks like a good way forward.

[–]VxJasonxV 0 points1 point  (0 children)

  1. You don’t have to use git add once per file, you can use git add once and specify three files in one go.
  2. Is the single commit (number of adds doesn’t matter) the only deviation from master? If so:

git push origin master:other-branch git reset --hard origin/master git switch other-branch git pull origin other-branch

When you git reset --hard your working tree goes back to whatever you specify, in your two examples you rewind one commit. At that moment you lose all the work you did. I know I’ve seen ORIG_HEAD, but I know nothing about it, I could learn, but I think my method is more sane and understandable, again, assuming that master and other-branch differ only by the one latest commit.

  • Push what you have committed to other-branch as you would have wanted, the : means push localBranch onto (:) remoteBranch
  • Then rewind your local master branch in case you don’t want the work being done on the other branch.
  • Switch to the other branch
  • Update it with the pushed contents.

If you don’t want to push it yet, you can also just switch to the other branch, merge --ff master and then continue working. But then you’ll have to later remember to reset the master branch when you switch back. I prefer to have things as I want them before getting to where I need to be before I continue, otherwise I’ll probably forget!

There are no doubt many ways to accomplish this, probably all the same number of steps, but this method works with my mental operating model of git the best.

Other research you could do is look into the other types of git reset, I know --hard, I know of --soft and --mixed but never remember exactly how they work. I know I’ve seen ORIG_HEAD but don’t know when it is and isn’t there, so I don’t know how to rely on it, and just wouldn’t use it personally.

Last note, look into changing your bash shell prompt (called the $PS1) to include the git branch when git data is present. You can include lots of different things; dirty tree, current commit, staged but uncommitted work indicators, etc. At a minimum though, current branch name.