Reputation: 39859
I'm trying to play with Git branching in order to create feature out of the master, but I too often end up messing my git repository :/
I searched on Stack Overflow about what I'm trying to achieve, and often I can find how to merge/branch/revert/etc but not in the workflow I'm facing, which is, for me, what can happen quite often.
Here's what I would do as a workflow :
Some of those actions seems easy, but the middle part (switching back, to master, apply change and then going again to branch) seems impossible to me :/
Here's where I am at :
(supposing the project is already on git, and some work has been pushed to master)
git branch feature
git checkout feature
# do some add/edit, commit, push on that branch
# Now I need to fix a bug in master :
git checkout master
# how can I get my files in the master state ?
# My best guest so far :
git revert HEAD
# do some add/edit, commit, push on master
# Finished to fix the bug, going back to continue my feature :
git checkout feature
# Again, how can I get my files to match the current state of the branch feature ?
git revert HEAD # And git asks me about a revert ? what ? I'm lost :/
# getting the recent changes from master into feature, in order to avoid later conflicts
# This can also be made often if you work with others
git fetch origin
git rebase origin/feature
git rebase origin/master
# do some add/edit, commit, push on master
# And when you have finish, merge feature into master :
git checkout master
git pull origin master
git merge feature
git branch -d feature # if you want to remove it.
This is my current supposed workflow, but I'm not sure revert HEAD
is a good solution, and when I go back, this is more confirmed by Git that is asking me for a revert to the previous state that I have to push (?!).
Can someone help me clarify my situation and/or direct me to a great (but please, very simple) explanation to do what I'm trying ?
Thank you a lot, I really appreciate !
Upvotes: 1
Views: 164
Reputation: 42094
git checkout master
should already “get your files in the master state”. The most common case where it fails is when you have uncommitted changes. But you could take care of that with git stash
. Or simply by committing to your feature branch, you can always cancel that one later.
git revert
is intended to cancel commits you can't eliminate anymore (published history, for example), and works by creating an additional commit to undo its effects. It's very different from the Subversion and/or Mercurial naming, and much less frequently needed.
Upvotes: 1