Practical example

If you're following the code for this book, you might have cloned it directly from GitHub. In that case, there should be a hidden (your operating system won't show it by default) .git folder inside—that's where all the old files are stored. You can access this folder at .git, or see it using ls -la command—but you shouldn't change it manually until you're sure you know what you're doing. As we don't want to break this repository, let's create a new project.

First, let's create another, empty folder—you can do that via the standard graphical interface on your machine, or via the Terminal. The following code will create a new folder and add a README file to it:

$ cd ../../
$ mkdir MyProject;
$ cd MyProject;
$ echo MyProject > README.md

Now, we need to initiate git in the folder, and add the README file via the first commit:

$ git init
$ git add .
$ git commit -m "My first commit"

Now, we have our first commit, that is, the stored state of the project that we can always revert to.

Now, try adding anything to README.md in the VS Code. As you may notice, the editor highlights new lines with a green line on the left and marks deletions with a red triangle – you can click on either to see how this part of the code appeared before the change. VS Code will only show changes to the last commit. Once you store the new version in a new commit, those lines disappear.

Similarly, VS Code will color files and folders in the Explorer menu to highlight those that you have changed. If you switch to the third tab—Git interface—you can select which files to add to the commit, and which you don't want toand then commit the change with the message. Now, let's try adding something to README and then committing this new change via the VS Code interface. This interface is quite handy, but not necessary for working with Git. For example, you can see the state of the repository by using git status in the command line. Furthermore, you can go through the history of the commits by using git log with one of multiple options, for example, oneline mode:

$ git log —pretty=oneline

6caa0f3ee610f50dc3fb48beec034a7870f7fb9 (HEAD -> master) another commit
5b0c88aa1a99a38d6c4d6db900d63c154c5127bf first commit

For you commit caches will be different! Now, let's go to the previous state by using the hash for the first commit:

$ git checkout 5b0c88aa1a99a38d6c4d6db900d63c154c5127bf

The preceding code will return you to the first version, but it won't allow you to then go back to the last one, or easily at least. Alternatively, you could return to the AND commit and create a new branch based on it:

$ git checkout -b old-state

Here, the old state is the name of this new branch, and is stored in parallel with the original branch, master. Feel free to work and commit other projects. Once you're bored with this branch, just go back to the master one, using the checkout command:

$ git checkout master
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.209.184