1

I'm new to git, below is my step, and want to know if I switch back to master why I see same thing modified on browser?

cd ../project location
git init
git add *
git commit -m 'without style'

git branch 'style-a'
git checkout 'style-a'
git show-branch -a

output

! [master] without style
 * [style-a] without style
--
+* [master] without style

... after I modified code inside file

and

git add *

then

git checkout master

why still get same thing on browser? do I miss something?

does use switch means I can switch to different branch or master to get each group updated code?

UPDATE

git status

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   ...
    more new file
user1575921
  • 1,078
  • 1
  • 16
  • 29
  • Possible duplicate of [How do you discard unstaged changes in Git?](http://stackoverflow.com/questions/52704/how-do-you-discard-unstaged-changes-in-git) – Basilevs Dec 15 '15 at 17:21
  • @Basilevs I still can't get it.. and I don't want delete the branch just wanna see the code before, what should I do? – user1575921 Dec 15 '15 at 17:36
  • What does `git status` give you? If it doesn't say "Working directly clean", you've still got unstaged changes floating about. – Makoto Dec 15 '15 at 17:48
  • I update git status result in question. unstaged changes floating about? – user1575921 Dec 15 '15 at 17:54

2 Answers2

1

When you create the style-a branch you are on master. Creating a branch in Git will start the new branch from where you currently were.

More precisely, you were on master and created a commit. So, your HEAD was pointing to the tip of the master branch. Then, you created a branch, and switched to it. But your HEAD is still pointing to the same commit as it was before because no new content was committed on style-a.

style-a is pointing to the same commit as master. This is why you are not seeing any new code changes.

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
  • Thanks for reply. so what is the correct step to get what I want : keep original files, and make changes to store on different new group (then I can make different version), and switch different version see each one – user1575921 Dec 15 '15 at 17:57
  • @user1575921 your terminology is not quite clear, but create commits on different branches depending on the use-case for that branch, and switch between branches with the checkout command, such as "git checkout master" – Jonathan.Brink Dec 15 '15 at 18:01
  • does that mean if I switch `style-a` make change code and do `git add *` and `git commit` then switch back to master will see original version? I have to commit under branch `style-a`, right? – user1575921 Dec 15 '15 at 18:07
  • @user1575921 correct...your commit will belong to the branch that you are currently on. Committing on style-a will have no effect on master – Jonathan.Brink Dec 15 '15 at 18:08
  • after `git checkout 'style-a'` , `git commit -m 'change'` and `git switch master` not work, still same result like 'style-a' .. – user1575921 Dec 15 '15 at 18:14
  • @user1575921 before you did your commit was there content staged and ready to be comitted? Did you do a "git add _filename_" first? – Jonathan.Brink Dec 15 '15 at 18:15
  • @user1575921 rather than "git add *" please pass the name of the file you are trying to stage, for example: "git add src/foo.txt". Also, prior to the add, are you sure that there were changes made to files in your local repo? – Jonathan.Brink Dec 15 '15 at 18:18
  • but if there are a lot lot files I have to add each path? and sure I make change to the files.. – user1575921 Dec 15 '15 at 18:21
  • @user1575921 yes...you can use wildcards, but I wanted to make sure you knew not to just always use the "*" or ".". I very commonly just do a "git add ." – Jonathan.Brink Dec 15 '15 at 18:24
1

If you want to temporarily switch to master just to look at the code you had, you can run git stash before switching branches to hide the work you currently have in progress ("unstaged changes"). Then when you switch back to your new branch, you can use git stash apply to bring your in-progress work back.

When you're ready to save your work, make sure you have the branch you want to save your work to checked out. Use git add <filename> to stage changes to a file (get them ready to save), and git commit to save your work to your branch.

Michelle
  • 2,830
  • 26
  • 33
  • thanks for reply , do I need to `switch` to `master` or `style-a` then do `git stash`? – user1575921 Dec 15 '15 at 17:59
  • You can use `git stash` anytime, although it's recommended you stash changes before switching branches. – Michelle Dec 15 '15 at 18:01
  • it works, then if I want to create more then one group/version like `master` `style-a` `style-b` use git stash how to switch to – user1575921 Dec 15 '15 at 18:03
  • You can create (`git branch`) and switch to (`git checkout`) your `style-b` branch in the same way you did `style-a`. The `stash` command just temporarily sets aside your changes until you bring them back; you can switch between whatever branches you want. If your changes are intended for `style-a` switch back to that branch before using `git stash apply`. – Michelle Dec 15 '15 at 18:11
  • I create branch `style-a` and make change then `add *`, `commit -m '..'` then switch back to `master` still same result.. do I miss somethin – user1575921 Dec 15 '15 at 18:15
  • What "same result"? 1. Do you mean your new code, that you committed to `style-a`, appears on `master`? 2. If you run `git status`, does it show any unstaged changes? If you run `git log` on each branch, is the commit history different? – Michelle Dec 15 '15 at 18:26
  • Thanks!! it working now. and the `stash` usage explanation! – user1575921 Dec 15 '15 at 18:35