Introduction
If you're someone who is struggling to get started with Git and GitHub, then you've come to the right place. Follow the steps in this article in order, and I hope that by the end of it, you will have a clear and basic idea of Git and GitHub.
Version Control Systems & GIT
Version control systems refer to a category of tools that helps a code developer manage changes made to the source code over a period. The tool keeps track of every modified version of the code. It does this with the concept of a repository. The repository is a database that stores all the versions of the code that have changed over time. This helps the developers revert the code to the earlier versions as and when necessary. Some of the widely used version control systems are GIT, Subversion, and Perforce.
Starting with GIT
Step 1 - Installing GIT & creating a GitHub account
The very first thing that needs to be done is to download and install GIT on the local machine. This link will help you with getting the required installation file for your system.
Post-installation, it is necessary to create a GitHub account.
N.B: While installing GIT for Windows, you will have options to either go for a GUI interface or a command-line terminal. I would recommend using the GIT Terminal for beginners as this will help them to get a knack for the commands used and help them understand the underlying mechanism of GIT.
Step 2- Create a Repository in your local machine
Assuming that you are using the terminal, let's create our first repository (or repo used colloquially).
You need to first open the GIT terminal and follow the steps below:
- Change the current working directory to the one you wish to set up your repository in by using the ‘cd’ command
![Git And GitHub For Beginners]()
- Creating a Projects folder:
![Git And GitHub For Beginners]()
- Initializing a GIT repository in the root folder using the git init command,
![Git And GitHub For Beginners]()
Step 3 - Adding Objects to the Project
We have now created a Project in the repository. Our next step will be adding a file in the Project path.
- We will achieve this by first creating a simple text file using the vi editor.
![Git And GitHub For Beginners]()
The ls command helps us to list the files in the current path.
- Using the git status command to see which files GIT knows to exist:
The output of this command tells us that GIT knows that file1.txt is there, but it's not going to do anything with it unless the git add command is used
-
Using the git add command to add files to the staging environment.
![Git And GitHub For Beginners]()
Step 4 - Going for the first commit
A commit is essentially a record of files you have changed since the last time a commit has been executed. So, when we perform a commit, Git moves all the files that are present in the staging environment to the repository.
Syntax
git commit -m "<comment>"
![Git And GitHub For Beginners]()
Step 5 - Creating a new branch
A new branch comes in handy when you want to perform some changes to the main code without affecting the main code. Suppose you have a new requirement and you do not want to change the main code. So, you create a new branch, check out your Project there, and keep performing the changes. Once when you decide to go ahead with the new code, you can merge it with the code in the master branch.
The following syntax can be used to create a new branch and check out the current version of the existing master branch.
Syntax
git checkout -b <new branch name>
The above code will also move the present working directory to the new branch.
![Git And GitHub For Beginners]()
In order to view the branches, the git branch command can be used.
![Git And GitHub For Beginners]()
The branch name with the asterisk next to it indicates which branch you're pointed to at that given time
Step 6 - Creating a new repository on GitHub
When you publish your local Git repository on GitHub, you can share your code and collaborate with other teams and people.
In order to do so, you first need to create a repository on GitHub.
- Log in to your GitHub account and go to its Home page, where you will be able to spot a Create Repository button:
![Git And GitHub For Beginners]()
- After clicking the Create Repository button, you will be redirected to the following page where you need to fill in all the necessary information:
![Git And GitHub For Beginners]()
- After you have created a repository, you have 3 options:
![Git And GitHub For Beginners]()
We will go ahead with option 2, and push an existing repository from the command line.
Step 7 - Pushing an existing repository
- Go back to your bash terminal
- Use the git checkout master command to switch to the master directory
- Use the git remote add origin command by replacing it with your repository URL. You will find it under the push an existing repository from the command line section
- Execute the following command: git remote add origin https://github.com/<username>/repo_name>.git
![Git And GitHub For Beginners]()
- The above will open a login window, proceed with entering your credentials and log in to your GitHub account.
- Then, execute the git push -u origin <branch_name> command
![Git And GitHub For Beginners]()
Step 8 - Pull Requests
A pull request is an alert to the repo’s owner that you want to make some changes to their code. It allows them to review the code and make sure it looks good before putting your changes on the master branch.
![Git And GitHub For Beginners]()
You will get something like shown below after creating a pull request:
![Git And GitHub For Beginners]()
Step 9 - Merge Pull Requests
The green button to merge pull requests merges the changes made in your branch with the master branch.
Caveat
In some cases, the merge pull request button is grey. This means there has been a merge conflict. This is when there is a change in one file that conflicts with a change in another file and git can't figure out which version to use. You'll have to manually go in and tell git which version to use.
Sometimes, you'll be a co-owner or the sole owner of a repo, in which case you may not need to create a PR to merge your changes. However, it's still a good idea to make one so you can keep a more complete history of your updates and to make sure you always create a new branch when making changes.
Step 10 - Getting all changes back to your local machine
Now, in order to sync your changes with the local Git, use the git pull origin master command.
Command
git pull origin master
![Git And GitHub For Beginners]()
Step 11 - View all the new commits
This can be achieved using the git log command.
![Git And GitHub For Beginners]()
That’s all folks!! Congratulations, on executing a Push, Pull and merge request on Git and GitHub. I hope this will help you get started with Git and GitHub. Happy Learning :)