mtchris mtchris - 1 month ago 8
Git Question

Understanding git branches

Newbie git branch question

If I have a simple page like

<!DOCTYPE html>
<html>

<head> </head>

<body>

This is master

</body>

</html>


Then crete a new branch and swtch to that branch

git branch new-branch
git checkout new-branch


Then do something in that branch like

<!DOCTYPE html>
<html>

<head> </head>

<body>

This is master

This is in the new-branch

</body>

</html>


I thought this new-branch would be separate from the master and if I switched back to the
master it wouldn't show the content added in the new-branch

If I checkout the master

git checkout master


It still shows the content added in the new-branch.

Can anyone explain why this happens.

mab mab
Answer

You just forgot to commit the changes you made in new-branch.

git checkout new-branch

# Do your stuff in <edited_file>

git add <edited_file>
git commit -m "A short desc. of your changes"
git checkout master

NB: You should have first commited your initial file into master branch. Full version:

git init .

vim my_file          # Create some initial content in my_file

git add my_file
git commit -m "My first file"

git branch new-branch
git checkout new-branch

vim my_file          # Add some line to my_file

git add my_file
git commit -m "Some new lines"

git checkout master  # my_file in master does NOT include changes made the second time
Comments