0

My team is using a very simple Git workflow. Everybody develops in the same branch. I'd like to know which are the series of commands I should do when I'm in the middle of the work and need to fix a bug.

Here is my situation:

  1. Unpushed commited changes in my local master branch
  2. Uncommited modified files
  3. need to fix a bug in origin/master branch
  4. I don't have another branch attached to master
  5. need to push just these files that fix the problem
  6. go back to work

Se that I'm working in the same branch of my coworkers.

Some users pointed that git stash would solve it and so this is a duplicate question. I dispute it, since I didn't even know that this feature existed. The feature solves my problem, but I believe the question is valid since I ask it from another point of view.

neves
  • 33,186
  • 27
  • 159
  • 192
  • Possible duplicate of [When should I use git stash?](https://stackoverflow.com/questions/20537223/when-should-i-use-git-stash) – underscore_d Jul 04 '17 at 15:55

4 Answers4

2

You have to stash the files

  1. git add . Add stuff like you normally would
  2. git stash stores it for later
  3. git checkout main do your work
  4. git commit commit your changes in main branch
  5. git checkout your_branch after you're done with that branch go back
  6. git stash pop brings back your work
cst1992
  • 3,823
  • 1
  • 29
  • 40
Adam LeBlanc
  • 932
  • 7
  • 21
2

Other answers have used git stash, which I agree is the standard technique, but here's another which "simulates" a stash (and has a backup since stashes aren't saved in your reflog; plus your main branch will not have your untracked files).

Here are the steps you should do:

1. git add .; git commit -m "All files saved" // commit your work in current branch
2. git checkout main // go to main branch
3. git add .; git commit -m "Finished fixing bug" // fix bug and commit
4. git push remote main // push your main branch to remote; do a fetch and merge, etc if needed
5. git checkout my_branch // come back to your branch
6. git reset HEAD^ // go back from your dummy commit

This will also save your untracked files.

Alternatively, use git stash -u, which also stashes untracked files.

cst1992
  • 3,823
  • 1
  • 29
  • 40
0

you need to stash your changes:

git stash save "will come back in 5 minutes"
git checkout blahblah
jadajadajada
git checkout where-i-was
git stash pop

You are back you where you were

eftshift0
  • 26,375
  • 3
  • 36
  • 60
0

git stash might be what you're looking for.

Use git stash to temporarily store the changes you've made to tracked files. Go away and work on another branch. Then come back and do git stash pop.

The requires memory. You have to remember that you stashed stuff away.

Alternatively you can commit your work, go away work on another branch then come back and git reset HEAD^ to put the branch in the state it was before. So in effect putting a stash in context. So long as you don't push this work in progress that you committed you're OK.

powlo
  • 2,538
  • 3
  • 28
  • 38