Section 14.2 Intro to Git
Git is a version control system that allows you to track changes to files over time. It allows users to track changes to files, revert to earlier versions, and collaborate with others. Git is a distributed version control system, which means that the entire codebase and history is available on every developer’s computer, which allows for easy branching and merging.
Subsection 14.2.1 The History of Git
Git was created by Linus Torvalds, the creator of Linux, in 2005. He created Git because he was unhappy with the existing version control systems available at the time. He wanted a system that was fast and simple. Git was designed to be a low-level version control system, which means that it is designed to track changes to files, but it did not provide a graphical user interface - however, there are now many GUIs available for Git. Instead, Git provides a command-line interface, which allows users to perform any action that they want. This allows users to create their own workflows and tools that work best for them.
Subsection 14.2.2 Repositories
A repository is a collection of files and the history of those files. A repository can be thought of as a folder that contains all of the files and folders in a project, as well as the history of those files.
For most projects, you’ll be setting up a repository on GitHub, which is a website that hosts Git repositories. There are other alternatives out there, such as Bitbucket and GitLab, but GitHub is the most popular.
Creating a repository on GitHub requires the following steps:
Select the New button in the top left corner of the screen.
Enter a name for your repository. This should be a short, descriptive name that describes the project.
Select the privacy level for your repository. If you want to share your code with others, you can select Public. If you want to keep your code private, or if you want to share your code with a select group of people, you can select Private.
If you want to add a README file to your repository, that is a document detailing additional information about your project, you can select the checkbox.
Consider adding a .gitignore file to your repository. This is a file that tells Git to ignore certain files and folders. This is helpful for hiding things like passwords and other sensitive information, as well as files that are generated by your code that you don’t want to track.
Also consider adding a license file to your repository. This is a file that tells others how they can use your code. There are many different licenses available, but the most common is the MIT license, which allows others to use your code for any purpose, as long as they give you credit. A similar license is the GNU General Public License, which allows others to use your code for any purpose, as long as they give you credit and allow others to use their code for any purpose.
Select the Create repository button.
Subsection 14.2.3 Cloning a Repository
Cloning a repository means downloading a copy of the repository to your computer. This allows you to make changes to the files in the repository, and then upload those changes back to the repository. This is the most common way to work with Git.
To clone a repository, you’ll need to have Git installed on your computer. You can download Git from
git-scm.com/downloads. Once you have Git installed, you can clone a repository by running the following command:
git clone <YOUR_REPOSITORY_URL>
You can find the URL for your repository by selecting the Code button on your repository’s page on GitHub, and then selecting the HTTPS option. All Git repositories hosted on GitHub end in .git.
Subsection 14.2.4 Staging and Committing
Staging a file means telling Git that you want to include the file in your next commit. A commit is a snapshot of the files in your repository at a given point in time. A commit is a way to save your work, and it allows you to revert back to that point in time if you make a mistake.
To stage a file, you can run the following command:
git add <FILE>
To stage all of the files in your repository, you can run the following command:
git add .
In order to verify that the files have been staged, you can run the following command:
git status
This will show you all of the files that have been staged, as well as all of the files that have been modified since the last commit.
Once you have staged all of the files that you want to include in your commit, you can run the following command to create a commit:
git commit -m "YOUR COMMIT MESSAGE"
The commit message should be a short description of the changes that you made. If you’d like to add a second line to your commit message, you can tag on the -m flag again, followed by another message.
Subsection 14.2.5 Pushing and Pulling
Pushing a commit means uploading the commit to the repository on GitHub. This allows others to see the changes that you’ve made.
To push a commit, you can run the following command:
git push
Up to this point outlines the basic workflow for working with Git. You can make changes to the files in your repository, stage the files that you want to include in your commit, create a commit, and then push the commit to GitHub.
If you are working with others, or working on multiple computers, you may need to pull changes from GitHub. Pulling changes means downloading the latest changes from GitHub. This allows you to see the changes that others have made, and it allows you to work on the latest version of the code.
To pull changes from GitHub, you can run the following command:
git pull
Note 14.2.1. Rule of Thumb.
A good rule of thumb is to always pull before you start working on a project, as well as before your commit and push your changes. This ensures that you are working on the latest version of the code, and that you don’t have any conflicts when you push your changes. Conflicts and how to resolve them will be covered in a later section.
You have attempted
of
activities on this page.