4

recently we moved from TFS to GIT so I am still learning how to use this amazing tool.

At the moment I am working in a very simple structure like the following one:

main -------------------------------------- 01 ----------
\                                           /
 dev ----------------------- 01 ---------- 02 ----------
  \                          /             /
   feat/login ------ 01 --- 02 --- 03 --- 04 --- 05 ---
  • Developers work on feature/something and every tot hours they check-in and trigger a build with tests
  • When the feature is stable they merge back into dev which will contains 1 or more checkins from feature/something
  • When the whole feature is ready, they merge the feature from dev into main

The questions are the following:

  • If I start to work from feat/login, if I do git checkout dev I don't get the latest code from dev but also my latest local file changed in feat/login. How can I checkout dev without carry over the local changes not commited from my previous branch?
  • Assuming a developer made a mistake, how can I rollback to a previous check-in 01 in dev and push it so that the latest check-in 02 is not the last one anymore?
4
  • 3
    You either need to stash your changes before checking out a different branch, or commit them. Commented Apr 13, 2017 at 12:36
  • 1
    For the first question, are you asking how to remove those local changes when switching to dev? If you want to keep them you either have to commit them or stash them before switching, but if you simply want to delete them just do git reset after switching. Bear in mind that when you later switch back, they won't magically reappear, that's why you have to commit or stash before switching. Commented Apr 13, 2017 at 12:37
  • Super quick! Guys write an answer so I can mark it as correct. git reset and stash, I didn't know :-)
    – Raffaeu
    Commented Apr 13, 2017 at 12:37
  • For the second question, is the mistake so horrible that you want to remove the whole commit? That's history rewriting, not recommended if you're still learning git. If the mistake can be left in the repository, you just want to undo its effect, use git revert SHA-of-commit to create another commit that reverts the effect of the one you want to undo. Commented Apr 13, 2017 at 12:38

2 Answers 2

10

1) Stash your changes, checkout to another branch, make some changes there and come back to your original branch and apply/pop your stash.

$ git stash
$ git checkout dev

# do something in you dev branch

$ git checkout feat/login
$ git stash apply

You can view your stash list using the command $ git stash list

2) It seems like you want to revert back the changes. I recommend you to not play with git history as a beginner. Anyways, if you do want to try it out here's a good answer how to do it. As Lasse said it's better to create another commit that reverts the effect of the one you want to undo.

2
  • 1
    perhaps it's worth noting that stash won't work with untracket files - if you have untracked files you don't want to "carry over", you have to commit them first, or stage and then stash
    – Dunno
    Commented Apr 13, 2017 at 13:28
  • 7
    Quick tip: you can also stash untracked files using git stash -u
    – Saugat
    Commented Apr 13, 2017 at 13:28
3

Another feature that you may use, is git worktree (https://git-scm.com/docs/git-worktree) which allows you to have working copies of multiple branches at the same time, so e.g. lets assume you are on master:

git worktree add -b dev ../dev origin/dev
git worktree add -b feat_login ../feat_login origin/feat/login

will give you 2 additional working copies in ../dev and ../feat_login that contain a working copy of the dev and feat_login branch.

I find this feature especially useful in projects were I have to switch between branches very often.

Not the answer you're looking for? Browse other questions tagged or ask your own question.