git branch - Git - switch local branches various scenario -
git powerful tool helps boost productivity. say, great power comes great responsibility, don't want mess while being more productive using git. need have strategies while using git version control. following few scenario while using local branches. can someone, please, explain how can deal those: , b local branches x remote branch
1.a pulled x, made changes. not staged,not committed,not pushed. how switch branch b without discarding changes of not including them in b
2.a pulled x, made changes.few unstaged(yes/no), staged changes,not committed,not pushed. how switch branch b without discarding changes of not including them in b
3.a pulled x, made changes.few unstaged(yes/no), staged changes,committed changes,not pushed. how switch branch b without discarding changes of not including them in b
4.a pulled x, made changes. want keep copy of these changes , want work simultaneously on other 2 features on top of changes in (the reason i'm working on multiple features simultaneously because might waiting hear further approach in feature , dont wanna waste time)
5.a pulled x, made changes. pushed remote. want older version without these changes go approach same feature.two sub scenario here: a)no changes have been pushed after pulled x , before pushed remote. b)someone pushed new code after pulled x , before pushed remote.
it might true few cases have same solution , few cases might moot/invalid m posting them sake of understanding why moot/invalid , sake completeness. on high level i'm trying understand behavior of stash,staging commit , have handy reference instead of spending dev hours juggling git can work on different features @ point of time. prefer not create new workspace(eclipse)/working directory(git) part of solution.
here answers:
there 2 general strategies switching branch while working directory not clean. first (and conceptually easiest) option
git stash
on branch a. place changes (staged and unstaged) since last commit stash object can applied later. second optiongit commit
. although seems second option violates question, bare me moment. when return branch @ later time, can continue working , commit new changes usinggit commit --amend
. history of clean in won't have separate commit earlier changes.- and 4. see answer 1 above.
if want work on earlier commit, can checkout earlier commit. typically want create new branch in scenario, command
git checkout <sha-1> -b newa
,sha-1
hash of earlier commit, trick. regard 2 sub-cases, won't matter whether else pushes new changes head of on remote or not. reason new branchnewa
(based on earlier commit of a) rewrite of history. way ever able push new branch remote doinggit push --force
.
as @cjm628 mentioned, should spend time reading git documentation, answer give starting point.