简体   繁体   中英

Git & automatically merging branches with master branch

I'm working on becoming more acquainted with git. I'm the sole person working on a project here at work, so I've gotten by with just making changes to the master branch all of the time but I need to step it up and be more fluent in working with branches. So yesterday I created a branch (QF1), switched to it and started to make some changes. I noticed something though when I switched back to my master branch: it switched branches apparently and presented me with a list of files preceded by "M"s (by the way, although the output below says that the files are staged at this point, they were not at the time I executed my first "git checkout master"). So after doing some research I found that the Ms means that git has merged the changes I have done in the QF1 branch into the master. I didn't want that to ever happen. I'm still a little foggy on this but I'm plowing through git-scm.com to better understand it. I thought that switching branches would pull the master's copy of things back into my working directory, but my editor doesn't tell me that this has happened (gvim).

  1. I don't want this to ever happen again. Is there something in git I can set to tell it not to switch branches if there are changed files in the working directory that have not been committed or at least added to the branches index?
  2. Do I need to use "git reset" here to point my master branch back to the last commit I did for master? I've not committed anything here, just done a "git add".

     $ git status # On branch QF1 # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: AuthNotifyResponse.cpp # modified: AuthNotifyResponse.h # modified: DataElementsTransactionResponse.cpp # modified: DataElementsTransactionResponse.h # modified: TransactionSession.cpp # modified: authNotifyResponse.xsd # modified: xch.h # $ git checkout master M AuthNotifyResponse.cpp M AuthNotifyResponse.h M DataElementsTransactionResponse.cpp M DataElementsTransactionResponse.h M TransactionSession.cpp M authNotifyResponse.xsd M xch.h Switched to branch 'master' $ git checkout QF1 M AuthNotifyResponse.cpp M AuthNotifyResponse.h M DataElementsTransactionResponse.cpp M DataElementsTransactionResponse.h M TransactionSession.cpp M authNotifyResponse.xsd M xch.h Switched to branch 'QF1' 

I don't really want to commit changes to QF1 since they relate to a project another group here is working on and it is not finished for them. I can commit them and amend the commit later if that's what I need to do.

Maybe I can just unstage the changes in master? I didn't really stage them in master to begin with, just in QF1.

The "M" doesn't mean merged it means modified. The reason this happened is because you made changes to local files and then switched the working copy back to the master branch without doing anything to the changes, hence the changes stayed around. To avoid this you must commit your work before switching branches.

You state that you want to get better at using branches. This is one of the things you must learn while working with branches. Commits are for you first and for others last. When you make a commit, you're taking all of your changes and recording them. If you don't want your changes, just discard them. But if you do want the changes, commit them, even if they aren't done. Simply don't publish the branch for others to see.

If you're worried about others seeing a bunch of checkpoint commits, you can also engage in sausage making before you push.

In essence, what you are doing is creating a feature branch and simply not completing the feature before you move on to something else. This is exactly why feature branches exist. Don't resist it, work with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM