0

I have two branches:

----master
  |
  |----lf

I developed some new feature in branch lf and get some output. Then, I tried to switch back to master (through git checkout) and execute the program to compare the result. Unfortunately, git checkout master will update what I did in lf:

M   Makefile
M   src/mainloop.c
M   src/threadpool.c
M   src/threadpool.h
Already on 'master'
Your branch is up to date with 'origin/master'.

As a result, I get the exact same project in two branches. This is not what I want. I don't want master auto update what I changed in lf.

My question are:

  1. How to recover the code in master before I do branch (git branch lf)?

  2. How to switch between different branches with no auto-update?

ErikMD
  • 13,377
  • 3
  • 35
  • 71
Steven
  • 811
  • 4
  • 23
  • doing a git checkout does not do merges. Are you sure you didn't merge? You should be able to view history of master and roll it back – mituw16 May 13 '20 at 11:44
  • I am pretty sure the only thing I did is switch from `lf` to `master` through git checkout master, then it auto update the new code in `lf` – Steven May 13 '20 at 11:50
  • @Steven what you describe definitely sounds like a normal behavior; see my answer for more details. – ErikMD May 13 '20 at 12:04
  • 1
    Related: [Checkout another branch when there are uncommitted changes on the current branch](https://stackoverflow.com/q/22053757/1256452) – torek May 13 '20 at 18:22

1 Answers1

4
  1. How to recover the code in master before I do branch (git branch lf)?

Do a commit (e.g. git commit -a -m "…") before doing git checkout master.

Otherwise, do git stash if you don't want to do a "temporary" commit. Later on, do git checkout lf && git stash pop to retrieve your uncommitted code.

  1. How to switch different branch with no auto up to date ?

Basically there is no "auto-update": the cause of what you observe is just that your working directory is not clean (i.e., there are some uncommitted modifications), and changing branch doesn't auto-update anything, it just keeps these modifications as is (otherwise that would lead to data loss!)

Also, a handy command that you may want to run anytime to get more insight on the current state of your current-branch/index/working-directory is: git status.

ErikMD
  • 13,377
  • 3
  • 35
  • 71