Git is a distributed version control system intended to allow developers to track changes on all files within their projects. Git was designed to enable developers to coordinate new features and bug fixes over different branches among multiple people.
First, check whether you have Git installed on your computer. In your Terminal or Command prompt, type
Git --version. If installed, you should see an output similar to what’s shown in the image below:
If you don’t have Git installed, please go to git-scm.com for the download and instructions on how to install Git on your Operating System.
A new project using Git will need to have a new repository created. To create your first repository on your local machine, in your Terminal, make sure you are within the directory you intend on using as your project directory, then run the following:
This command will create a
.git directory. This directory contains the information necessary for your project to be version controlled. It also includes the information about the project’s branches, commits, and if you host your git repository remotely, the address for this remote repository. Having all of this information allows you to know which branches you can switch between for different purposes, as well as rolling back to various commits in the history if necessary.
At this point, your repository is empty, so, to add some content to the repository, create a file in this project directory. It can be anything you wish. For example, a
README.md file, which is where people tend to start when they first come across a repository + it’s the default file shown on a Git repository service such as Github or Bitbucket.
Again, in the command line, but you can use a text editor or an IDE to create new files to your project.
touch README.md echo 'This is the README file for my first Git repository' >> README.md
If you then open your file, you’ll see the text
This is the README file for my first Git repository.
At this point, your Git repository is still technically empty. You’ve created the file but, if you type
git status you’ll see something similar to the image below:
As shown in the image above, the first line tells you which branch you’re in, and the second line explains whether you have any unpushed commits. Following this, you’ll see any untracked files which list the files you’ve yet to commit; in this instance, it’s the
Now it’s time to add this file to commit it to history.
git add README.md git commit -m "This is my first commit message"
So far, you’ve created a new repository, then added your file to this new empty repository. What if you wanted to make a change to your file but fear it may break something? Or you have multiple people contributing to your repository and want to have some control over what ends up in your main primary branch? Using branches comes in handy here, to allow different versions of the code to exist at one time.
You can create a new branch by typing the following command:
git checkout -b <branch name>
If you wished to see what branches are available, type:
git branch to list the branches you have locally on your machine.
Your new branch at this moment is an exact copy of your primary branch. Create a new file to this new branch to make it different from the primary. The example below creates a
README2.md file with a message within it:
touch README2.md echo 'This is the README2 file for my first Git repository, in the second branch' >> README2.md
Now commit this change with the following:
git add README2.md git commit -m "This is my second commit message."
Your new second branch is now out of sync with the primary branch. If you want to see the differences between the two branches, run the command similar to:
git diff main..feature-a replacing
main with your primary branch name and
feature-a with your second branch. The output will be similar to below where it shows a
README2.md file exists in one branch but not the other.
diff --git a/README2.md b/README2.md new file mode 100644 index 0000000..90372ea --- /dev/null +++ b/README2.md @@ -0,0 +1 @@ +This is the README2 file for my first Git repository, in the second branch
Check out back into your primary branch with the command:
git checkout <primary branch name>.
Now it’s time to have a look at GitHub and hosting your repository remotely.
Github is a remote hosting platform to store your Git repositories. This service allows you to control your version control on a third party and collaborate with others on projects.
Head over to GitHub to create a new repository. You will be presented with a screen similar to what you see below. Make sure to add the
Repository name and then click on
Once created, some options are provided with how to get set up with your repository. As you already have the repository locally, choose the third option below where it says
...or push an existing repository from the command line.
You’ve now created your GitHub repository and pushed your primary branch to the remote repository. Next, push your second branch by typing the following command in your Terminal:
git push -u origin <branch name>
Then, in Github, navigate to your repository, click “Create Pull Request”, and click on your second branch name. You’re taken to a page with a list of changes. These changes show the commits and the files changed. Again, click on the “Create Pull Request” button to be taken to a page with two input boxes.
This part of creating a pull request allows you to describe the changes you wish to be merged into the primary branch. It is good practice to be as descriptive as possible in the description box. This page will allow anyone to understand what the changes are and how they impact the current project.
Once you’re happy with the title, the description, and if you wanted to add anyone to review your pull request. Click the “Create Pull Request” button.
Now you or your co-contributors can review the changes and determine whether they should be merged into the primary branch.
If you are happy to merge this branch, click the “Merge Pull Request” button. You have now merged your changes into the primary branch.
If you go back to your Terminal, make sure you are on the primary branch, then type
git pull, you’ll see the file you added to the second branch is now in your primary branch!
If you have followed this tutorial from start to finish, you have now installed Git on your local machine, added some files, and committed these files to the repository history. You then branched out from your primary branch, added some more files, and again committed these files to the repository.
Following the above steps, you created a GitHub account and a repository on GitHub. You then linked your local repository to your GitHub repository and pushed your branches, changes, and commits to this remote repository.
Next, you created a
Pull Request, which highlighted the differences between your two branches, and allowed you to merge the secondary branch into the primary, so that now all changes exist in the primary branch.
With Hacktoberfest just around the corner, there’s no better time to put your Git learnings into practice! We are excited to be a Hacktoberfest partner this year, so you might want to check out some of the Vonage projects while you work towards your PR goal. Happy hacking!
Don’t forget, if you have any questions, advice, or ideas you’d like to share with the community, then please feel free to jump on our Community Slack workspace, or pop a reply below. I’d love to hear back from anyone that has implemented this tutorial and how your project works.