0

In my work, there are many independent bugs or necessary configuration changes being reported all the time. I have a copy of the repository which contains the code I need to edit to accomplish these changes. These changes are all independent of each other. I would like to be able to make changes on one branch, save them, and then switch to a different branch and work on unrelated changes without bringing the previous changes with me.

I thought that this pattern was more or less what git was designed to do, but git doesn't seem to keep the branches separate. I just executed the following:

git checkout -b me/new-branch
vim <file>
git add <file>
git commit <file> -m "message"
git push origin me/new-branch

And the resulting PR had 3 commits in it: the newest one, and two random ones from a week ago. This is a constant problem; I can't figure out why any particular commit ends up in any particular PR. The commits being put into multiple PRs aren't necessarily the most recent ones I've been working on. I can use the same git commands three times in a row, and two of the branches I create will be "clean" (not include extra commits when I push them) and the third one will have multiple commits in it. I'm sure git is behaving as intended, but I have no idea what it's doing or why.

Here is exactly what I am looking for:

  1. I'm clearly missing some very basic principle of how git works. What is it?
  2. Is there a series of git commands I can use to see if I currently have work that will follow me from one branch to another? I've been using git status but that doesn't work.
  3. What is the intended way for me to save my work on a current branch so that I can pick it up later without taking it with me when I switch to working on a different branch?
  4. How can I consistently make new branches that are just copies of main with no other changes?
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
WithScience
  • 103
  • 3
  • Do you know what commits are? Consider using a visual git tool like `ungit` to show you the commit tree. – evolutionxbox Sep 20 '22 at 22:13
  • 2
    You are doing everything correctly, except you are always creating a new branch at the latest commit instead of what was the latest commit before you fixed an unrelated bug. – mkrieger1 Sep 20 '22 at 22:15
  • "What is the intended way for me to save my work on a current branch so that I can pick it up later without taking it with me when I switch to working on a different branch" Yes. It's called "commit". – matt Sep 20 '22 at 22:16
  • @mkrieger1 thank you, I think that's the root of the misunderstanding. So if I'm understanding this correctly, I need to commit everything, then switch back to main (ie, git switch main), and then create a new branch – WithScience Sep 20 '22 at 23:09

0 Answers0