6

6Git & Brackets a worked example

6.6

Switching branches

This is a small but important point.

If I’m on the d-01-intro branch I have these files in the file tree and the following commits:

Figure 6.50 - Files and commits on the d-01-intro branch

Figure 6.50   Files and commits on the d-01-intro branch

I clearly have the 01-intro.html file and there are three commits.

Change back to the master branch (by clicking the small arrow next to the branch in the file tree and selecting master, Figure 6.51).

Figure 6.51 - Brackets—switch branches

Figure 6.51   Brackets—switch branches

Now I get:

Figure 6.52 - Files and commits on the master branch

Figure 6.52   Files and commits on the master branch

The 01-intro.html file is no longer there and there are only two commits.

To reiterate what I said in section 2.3.1:

SWITCHING BRANCHES CHANGES THE FILES
IN YOUR WORKING DIRECTORY

This is important. Like I said “you’ll remember it when you’re on the wrong branch wondering where all your files have gone”.

6.6.1

Switching branches with uncommitted changes

I said before, “you can’t switch branches if you have uncommitted changes on that branch”.

Switch back to the d-01-intro branch and open the 01-intro.html file. Add some text into the <h3>Introduction</h3> line (line 13) and save the file. The file status will now be modified:

Figure 6.53 - Modified file on d-01-intro

Figure 6.53   Modified file on d-01-intro

If I now try to change back to the master branch I get the following message:

Figure 6.54 - Brackets switch branch error

Figure 6.54   Brackets switch branch error

Brackets won’t let you change branches if you have uncommitted changes.

Again this confirms what I said in section 2.3.2:

YOU CANNOT CHANGE BRANCHES IF YOU HAVE
UNCOMMITTED CHANGES IN YOUR WORKING COPY

Always commit changes before switching branches.

See the footnote in § 2.3.2 for the reasons why.

6.6.2

Discarding the latest changes

I made the modification to 01-intro.html to prove the above point; I don’t really want to make these changes. However, I’ve saved the file so how do I go back?

The answer is simple, click the more actions button terminal icon and select discard all changes since last commit.

Figure 6.55 - Brackets—discard changes

Figure 6.55   Brackets—discard changes

Click ok, the file will go back to how it was and Git will show:

Nothing to commit, working directory clean.



End flourish image