Photo by Roman Synkevych on Unsplash
Getting Started With Git And GitHub
Begin your Version Control And Code Repository the Right Way
Git is a free and open-source version control system that helps you keep track of changes made to your code.
It was created by Linus Torvalds in 2005. Now, unlike older centralized version control systems such as SVN and CVS, Git is distributed: every developer has the full history of their code repository locally
One interesting thing about Git is that it allows you to create branches of your codebase so that you can work on new features or bug fixes without affecting the main codebase.
Imagine you are working on a project and it is already in use, now, someone comes along and makes a suggestion that might improve the functionality of what your code does.
For instance, you built an app that helps people search for their long-lost classmates by inputting their names into a search field and that crawls the web and returns search results that you can select from, Let's assume our hypothetical app also has a field where you can upload a picture of that classmate to aid the search and be able to get a more accurate result.
You have finished the code and it works perfectly, now Somewhere along the line you start getting feedback that the pictures are usually old ones and you can't easily match them with the classmates' new pictures.
You see that you need to build in a new feature that can take the old picture you uploaded and try to go forward in time to see what the person might look like in the present day.
Now you go back in and try to rewrite your code and mistakenly break something and the app is a mess.
Instead, you could use git to create a branch that is you can copy your original code into another place without tampering with it and then write the new feature into the code and then see if it works perfectly.
Once you’re done with your changes, you can merge your branch back into the main codebase. If it doesn’t work you can discard the changes and work on another solution
Git also allows you to collaborate with other developers by sharing your code repository with them. You can use Git to pull changes made by other developers into your local repository and push your changes back up to the remote repository
Git and GitHub although often confused are quite different from each other. To get an idea of what I mean.
Git is a version control system that manages and keeps track of your code
Think of the example above, our hypothetical people finder app, git helps us keep track of all the different versions of the app and the new features, you know where you are and you can go forward and backward without breaking your main codebase.
GitHub, on the other hand, is a service that lets you host, share, and manage your code files on the internet
Think of it like an online storage like your Google Drive or Dropbox but this is for only storing, sharing, managing and hosting the code you wrote.
Git can be used locally or remotely, while GitHub is only available as a remote platform.
Git is an open-source software development tool used for collaboration and version control.
It allows you to create branches of your codebase so that you can work on new features or bug fixes without affecting the main codebase. Once you’re done with your changes, you can merge your branch back into the main codebase.
GitHub is a web-based Git repository hosting service that makes it easier to use Git and adds extra functionalities. It provides all of the distributed version control and source code management
Git is a powerful tool and it will sure take some time to learn. However, there are many resources available online that can help you get started with Git.
Here are some basic Git commands that you can use to get started:
git init
: Initializes a new Git repository.git add
: Adds files to the staging area.git commit
: Commits changes to the repository.git push
: Pushes changes to the remote repository.git pull
: Pulls changes from the remote repository.
For more detailed information on how to use Git, you can check out the Atlassian Git tutorial which provides a comprehensive guide on how to use Git as well as other useful resources such as Git cheatsheets and learning resources.
Here are the steps to get started with Git:
- Download Git for your operating system.
Before anything, you need to download git depending on your operating system
For Windows Users
Go to the Git website at https://git-scm.com/download/win.
Click on the download link for the latest version of Git for Windows.
Once the download is complete, run the executable file.
Follow the instructions in the installer wizard to install Git on your computer.
During the installation process, you will be asked to choose between using Git from the command prompt or from Git Bash. If you’re new to Git, it’s recommended that you choose Git Bash as it provides a more user-friendly interface.
Once the installation is complete, open Git Bash or the command prompt and type “git version” to verify that Git was installed correctly.
For Mac Users
Open Terminal on your Mac.
Type “git --version” and press Enter.
If Git is not installed on your Mac, you will be prompted to install it.
For Linux Users
Open your terminal.
Type “sudo apt-get update” and press Enter.
Type “sudo apt-get install git” and press Enter.
Once the installation is complete, type “git --version” to verify that Git was installed correctly.
If you’re still having trouble installing Git on your computer, you can visit the GitHub page on how to install Git at https://github.com/git-guides/install-git or How-To Geek at https://www.howtogeek.com/832083/how-to-install-git-on-windows/.
- Configure your Git username and email address using the following commands:
$ git config --global user.name "Your Name"
$ git config --global user.email "youremail@example.com"
Remember to put your user name and email where you have “Your Name” and “youremail@example.com” respectively and press enter after each line
- Create a new repository on GitHub or GitLab.
Here are the steps to create a new repository on GitHub:
Go to the GitHub website at https://github.com/.
Log in to your account.
Click on the “+” icon in the top right corner of the page.
Select “New repository” from the dropdown menu.
Enter a name for your repository.
Choose whether you want your repository to be public or private.
If you want to add a README file to your repository, select the “Add a README file” checkbox.
Click on the “Create repository” button.
Here are the steps to create a new repository on GitLab:
Go to the GitLab website at https://gitlab.com/.
Log in to your account.
Click on the “+” icon in the top right corner of the page.
Select “New project” from the dropdown menu.
Enter a name for your project.
Choose whether you want your project to be public or private.
If you want to add a README file to your project, select the “Initialize repository with a README” checkbox.
Click on the “Create project” button.
- Clone the repository to your local machine using the following command:
$ git clone https://github.com/your-username/your-repository-name.git
Here’s an example of what you might see when you clone a repository in Git:
Cloning into 'my-repo'...
remote: Enumerating objects: 100, done.
remote: Counting objects: 100% (100/100), done.
remote: Compressing objects: 100% (75/75), done.
remote: Total 100 (delta 25), reused 90 (delta 15), pack-reused 0
Receiving objects: 100% (100/100), 1.00 MiB | 1.09 MiB/s, done.
Resolving deltas: 100% (25/25), done.
This message indicates that Git is cloning the repository named “my-repo” and is downloading all of the files and folders associated with it.
- Add files to the repository using the following command:
$ git add .
This automatically adds all the files you are working on to a staging area where they are kept temporarily.
- 6. Commit the changes using the following command:
$ git commit -m "Your commit message"
Here’s an example of what you might see after you commit your message in Git:
[master 1234567] Added new feature
1 file changed, 1 insertion(+), 1 deletion(-)
This message indicates that you have successfully committed your changes to the “master” branch and that one file was changed with one line added and one line deleted.
- Push the changes to the remote repository using the following command:
$ git push origin master
Here’s an example of what you might see after you push your changes in Git:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 246 bytes | 246.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
This message indicates that you have successfully pushed your changes to the remote repository.
YOu can now go to your GitHub and you will see all your changes have been implemented. In another post, we will go deeper into git and GitHub.