Introduction
As a beginner, I've always struggled to learn Git and GitHub. I learn one day and forget the other day. One of the biggest mistakes we make while learning Git and GitHub is to believe we have learned something after watching videos or reading documentation and running a few commands. Well, that's not it, it's about the daily implementation of your work. So here's a quick trick for regularly practising the fundamentals: practice questions and then upload them to GitHub. So let's get to the process.
Git and GitHub in layman's terms
Let's say you're creating a car. In this example, Git will save each step or phase of your development so you can always revert to a prior version if you're not happy with the results. Let's assume some of your friends want to add features to your car, but you are worried that they would damage it or that they lose the car. As a result, we will distribute copies of the vehicle, allowing others to modify or add features and come back to us once they are done. If the modifications are satisfactory, we will review them and allow them to include the new features in our original model. GitHub is where the car will be stored and through which your friends will modify the car.
In technical terms, Git is a version control system. It maintains a history of earlier versions of these files and keeps track of any modifications made to files in a directory. Git allows users to collaborate on the same code and combine their contributions into a single branch once they are done. GitHub is a repository hosting service which facilitates many features such as access control and collaboration.
Basic commands to use Git
Suppose you have a folder in which you have a project or you will build a project and you want to have every version of your project saved. This is where Git will come into action. So let's get into it step by step.
git init
This command starts a new git repository, or you could say it initialises git.
git status
This command shows the state of the repository and staging area. It displays which files are tracked and which are not yet. Every time you make a change it moves to the unstaged area.
git add
This command adds the changed file to the staging area. The staging area is files that are going to be a part of the next commit, which lets git know what changes in the file are going to occur for the next commit. It allows you to record small changes in commit. There are two commands we can use for git add i.e,
git add .
- it adds all the unstaged files to the staging area.git add file-name
- it adds only the specified file to the staging area.
git commit -m "commit-message"
This command takes everything from the staging area and makes a permanent snapshot of the current state of your repository with the message specified so that it is easier to track the changes.
For example : git commit -m "added margin to nav items"
git checkout filename
This command reverts back to the previous version of your file.
git diff filename
If we made changes to one of the files and we want to check the difference between the files or the changes we have made we can use this command.
git restore --staged file-name
This command is used to unstage files from the staging area. The alternative command for the same purpose is : git rm --cached -r
and it removes all the files from the staging area.
Commands to use GitHub
Suppose you have created a GitHub account. Create a new repository or the locally created project you want to push to GitHub. Copy the URL of the repository you have created.
git remote add origin URL
Here origin is the nothing but name of the URL. This command is used to add a remote repository to your local Git repository. Once you have added a remote repository, you can use Git to interact with the remote repository and share your changes with others. You can push your local changes to the remote repository, pull changes made by others, and merge your changes with those of others.
git remote -v
This command is used to list the remote repositories associated with a local Git repository. This command displays the names of the remote repositories and their URLs, allowing you to see which remote repositories you have set up and their locations.
git push -u origin main
In this specific command, you are pushing changes from your local branch named "main" to the remote repository named "origin", and the remote branch will also be named "main". By specifying the remote branch name, you are creating a new branch in the remote repository with the same name as your local branch.
git push
This command is used to upload changes from a local Git repository to a remote repository. This command allows you to share your changes with others, collaborate on a project, and keep your remote repository up-to-date.
git clone URL
This command is used to clone a repository from GitHub. This is used when you want to contribute to someone else's project or repository.
Branching in GitHub
git branch
- This command is used to display the branch you're in currently.git branch name-of-new-branch
- This command is used to create a new branch.git checkout branch-name
- This command is used to change the current branch to the specified branch.git merge branch-name
- This command is used to merge the specified branch to the main branch.git push origin branch-name
- This command is used to push the new branch that is created to remote.
Conclusion
The combination of Git and GitHub has become an indispensable tool for developers, making it easier to manage and track changes, collaborate with others, and ultimately deliver better software. Whether you're a solo developer or part of a team, Git and GitHub are essential tools that can help you take your coding skills to the next level.