First Branch and Commit | Bondar Academy
Course: Git and GitHub Fundamentals
Module: Working with Git and GitHub
Instructor: Artem Bondar
Lesson Summary
In this lesson, we cover the initial Git configuration , creating a first branch, and making the first commit using both the command line and Visual Studio Code interface. Initial Configuration Protect the master branch to prevent direct pushes. Go to Settings in your repository, then Branches . Add a branch protection rule : Set the branch name pattern to master . Check Require a pull request before merging . Uncheck additional approvers and check Do not allow bypass . Configuring Git Set your global user email and name using the following commands: git config --global user.email "[email protected]" git config --global user.name "Your Name" Validate the configuration with: git config --global user.email git config --global user.name Creating a Branch Create a new branch with git branch first_code . Switch to the new branch using git checkout first_code . Making Changes Create a new file (e.g., test_file.txt ) and save it. Stage the file with git add . . Commit your changes using git commit -m "Initial commit message" . Tracking Changes Use git status to check the status of files. Use git diff to see unstaged changes. Commit additional changes similarly. Reviewing Commits View all commits with: git log This displays commit history along with unique identifiers for each commit. In the next lesson, we will learn how to push the branch to the remote repository.
Video Transcript
All right, in this lesson, we will make initial git configuration, and then we will create a first branch and first commit. And I will show you how to do it using command line interface and using Visual Studio Code interface. So let's jump into it. All right, so let's create our first branch and first commit. Before we begin working with branches, let's configure a little bit our test repository. So we need to create a protection for the master branch to make sure we will not push to this branch directly. We want to use pull request, we want to use branches, how it's normally would be done in real collaboration work. So let's configure this, go to settings under your repository, then go to branches, and we need to add branch protection rule. Click on that, then type a branch name pattern. Our main branch has a master branch. This is the branch we want to protect. Then we want to click on this checkbox, require a pull request before merging. Click on that, and uncheck this checkbox about additional require approvers, like this, and scroll all the way down to enable this last checkbox. Do not allow bypass the above settings. Since you are the admin of this repository, even you will not be allowed to push to the master branch and bypass the settings we are configuring right now. So check this checkbox and click Create. You need to enter password to your repository, and the rule was successfully created. We can navigate back, and everything is all set. All right, let's go back to Visual Studio Code, and we begin working with branches. So the first thing, please check if your Visual Studio Code have activated autosave. So autosave will handle saving of the files automatically for you. And we begin with configuring of the Git. So first thing, we need to configure your global user email and global user name. Those settings will be used in the GitHub repository to identify you as a contributor. So when you commit and push to your repository or other repositories, this identification will help to identify who exactly contributed to this commit. So we need to perform this settings. In the command line, you type git config dash dash global. And first, let's set up user email. User.email space and provide email for your GitHub account. And in our example, it's git user, bonderacademy.com, and hit Enter. And the second settings is user name. Git config dash dash global user.name, and in double quotes, provide your name. I will type Artem Bondar since I'm working on this particular repository, and hit Enter. Now let's validate that it was set successfully. You just check user config global user email. Hit Enter. Email is displayed, and the same for user name. Hit Enter, and name is displayed successfully. All right, so we are all set, and now we can create our first branch to contribute to this project. And remember about the best practices of the Git. Always, always create a new branch when you begin contribution. Never work on the master branch or on any other shared branch. When you want to commit your own changes or your account, always create a new branch. So in order to create a new branch, we need to execute command git branch and provide the name of the branch. For example, it will be first code, and hit Enter. And now we can review if this branch was created. I type git branch one more time, hit Enter, and we can see that we have two branches right now. Master branch and first code branch. Now we need to switch to our branch to begin working on this particular branch. I type git checkout first code, and hit Enter. Switch to branch first code, and I type git branch one more time. And now we can see that little asterisk is displayed next to the first code branch. It means this is an active branch right now. Also, if you look into the lower left corner of Visual Studio Code, you can also see the name of first code branch is displayed over here. It means we successfully switched to this branch. And let's begin contribution. So for example, let's create in the root of the project a new text file. And I call it new file test file dot txt, and hit Enter. And now look what happened. After this file was created and saved, on the left next to the icon of the source control, we see a little number one button. It means git identified one file that was changed or added. I click on that, and this panel will display all the files that were updated, added, or modified. And you can always review what was changed in your code. So we created test file dot txt, and next to this file, you can see a little U icon. It says untracked. If I will type also in the command line, git status, and hit Enter, we also can see that, okay, files untracked, and we see test file dot txt. So for us, in order to add this file to version in control, right now it's not added to the version control. We need to execute command, git add, and put dot. A dot means that we want to add all files that are untracked or add all changes that are not added to the version in control. Hit Enter, and now files are added. Now look on the left what has changed. Test file moved to the staged changes. It means that we saved the changes, and those changes are ready to be committed. But they are not committed yet, and also the icon changed from untracked to a, which is added. And now let's make something in this file. So let's write some first sentence. This is a test phrase, for example. This change is saved automatically by Visual Studio Code, and what we have again. So git identify changes one more time. We see that this file was changed, and we also see this file in the staged changes. So to save those changes and add them again to the version in control, we need to run command, git add dot, or we can simply here click on the plus button, and those changes will be added to the previous changes as well. I click on the plus button, and changes are added as well. If I execute command, git status, we also see in the command line that this file was added to version in control. And if I type command, git diff, and it's not showing anything because we already staged the changes. So let me add one more change. So test phrase one. We have unstaged changes, and if I type git diff right now, here we go. Now we see the difference between the latest change and staged changes. This is the test phrase, and something that is not staged yet and not added to the version in control is the update to this file. This is a test phrase one. And if I type git add dot, now it's staged and added to the version in control and ready to be committed. So we created several changes, and now let's create a first commit. We execute command, git commit dash m, and then in double quotes, we need to type a message that describes what is it in our commit. And I remind you, the commit message should be pretty descriptive. Created txt file with one message, something like this, and hit enter. And commit was successfully created. And also, you can see that changes that we just modified disappear from the source control tab just because we saved our changes in the first code branch. And we created a snapshot of the current version of the code. So let's contribute to this file a little bit more and create a new commit. And I want to create a second phrase, and I write a new message. This is a second phrase. And look what has happened right now. When we created a first commit and started adding a new information to the file, Visual Studio Code, helping us by visual identification, what are the changes we are adding to the previous file? So since this is something new in relation to the previous commit, Visual Studio highlights those two lines of code with this green bar. This helps a lot when you navigate through the code. Imagine you have 100 lines of code and you're changing the code in the different places. Visual identification of things like this, what you have added into the code, helps a lot to navigate across big files to quickly look and see what did you change. And let's make this change one more time. So we need to stage those changes and write commit message. Also, you can alternatively write your commit message in Visual Studio Code directly here in this interface. So you can choose any approach, use command line or use user interface. And let's type, this is a second phrase. And I click commit. And those changes were saved successfully in the second commit. And let's say we want to perform one more modification to this file and replace a phrase with, for example, word, something like this. And look one more time, Git immediately identified that this file was modified. We also see it showed up in this panel on the left in the Visual Studio Code. And also look how it is highlighted. Right now, it's highlighted with this little blue bar, which also give us a clue that this particular line is not new one. This one is modified. So again, this is very convenient interface. When you work with big files, if something is highlighted with the green, it means that this is the code that you added into the relation to previous commit. If something is highlighted in blue, that it means that you modified this line of code in relation to the previous commit. So this is very useful to know. But now we need to add this again and create commit. Modified phrase to word and create commit. All right, so we created three commits and how to review those. So if we type command Git log and hit enter, now we can see all the commits that we have performed and author who made those commits. So this is the most recent one, modified phrase to word, then the previous commit and our initial very first commit. You can see also this long number. This long number is unique identifier of the commit on your branch. If you need to revert back in time to revert your changes to the previous versions of your commit, you can use this identifier to revert back in the history. And how to do that, we will talk later in the class. All right, we created three commits. All of them were done on the local branch with the name first code and everything is ready to push this branch to remote repository. And we're going to do it in the next lesson. All right, see you guys in the next lesson.