Git 101
Beginning with any online session, we hear everyone throwing out the phrase:
“You can get the project from my GitHub profile”, “Push the changes in Git” but what is Git really and what do these terms git clone, git branch and their numerous brothers or sisters mean.
So, let’s first begin with what Git really is:
Git
Git is like a diary in which you and your team can store all your code for the project and all the updates that you perform so that if you make a mistake down the line, you can easily go back to the previous version or as it’s official website states:
“Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.”
This saves 1000s of hours that may be spent just to revert the changes your friend made accidentally in the group project.
Setting up Git
In order to use Git locally on your computer, you’ll need to set it up first –
1. Download and install the latest version of Git.
https://git-scm.com/downloads
2. Open Git Bash
3. Set up your username
4. Set up your commit email address in Git
Now for making projects and tracking them, we’ll be using GitHub to make our lives easier and this blog shorter.
GitHub ??
Ok so we know what Git is but what about GitHub?
GitHub is like a platform where we can host our Git files. It acts like a library of all the projects that we can easily access and download.
This also acts as a platform for sharing codes, projects and also as a proof of credibility as each and every commit is saved and can then be accessed or reverted back to.
So, let’s see how we can create our own GitHub account and then how to begin with our new project:
Setting Up GitHub and beginning a project:
1. Go to https://github.com/
2. Follow the steps to setup / login to your account
(Don’t forget to enable 2FA for extra safety )
Now with that done, we can move towards the various scary “git” commands and try to simplify them. We’ll use the commands on our new project to make it simpler.
git Commands
git clone
This command is the beginning for our git project. This creates a folder (or directory) that contains all our files for the project locally which makes it easier for us to add, delete or modify files as we wish.
- Open “Command Prompt” by searching cmd in search bar.
2. Use the command “cd <directory address>” to open the folder where you want to save the git repository.
3. Now use “git clone <https address>>” to clone the repository into your system.
If you now open
Opening the location, we’ll find a new folder with the name of our GitHub project.
(If we enable the “view hidden files” option, we can see a .git folder which stores the commits and history of our project and even more files that are too complicated to explain here)
Congratulations! You’ve successfully cloned a Git repository to your local machine. Now you can start working on the project and make changes as needed. Remember to use git pull regularly to keep your local copy up-to-date with any changes made to the remote repository.
Git classifies any changes made into some stages that will help us to understand them better:
U — This refers to “Untracked File”. It simply means that file with this name didn’t exist in the previous commit.
A — This refers to “Added File”. It means the files that are ready to be committed in the next commit.
M — This refers to “Modified File”. It is for the files that existed in the previous commit but are now modified. (i.e., the file doesn’t match the file in the previous commit)
D — This refers to “Deleted File”. It is attached to the file which was present in the previous commit but isn’t present now.
To check this, we can type “git status” in the terminal or cmd (while we have the git repo folder opened). All the status of the files and commit will be displayed.
For the next few commands, let’s try 2 approaches —
Approach 1 -> manual labour by using cmd
Approach 2 -> smart and less work by using VS Code shortcut
Approach 1 (CMD)
git add
This command takes all the untracked files whose names you have mentioned and “adds them” i.e., prepares them to be committed.
If we use “git add .” it will add all the files that are untracked.
If you want to add only a specific file, you can also do that by “git add <file name>”
git commit
This command takes all the added files and begins the commit process.
- Type “git commit”
2. A “Vim Text editor” is opened which asks you to enter a commit message. (This message will be displayed in GitHub and will help you to understand what the commit did as you can include small details like — “UI modified” or “Added readme”)
3. Enter the message and click the escape key. This will bring the text pointer to the bottom of the command prompt and enter the “ ”
4. Type “:wq” in it. This saves the file and closes Vim.
5. Your commit will then be saved and also assigned the commit message and a unique number to identify it later.
git push
This command takes all the commits and then pushes them to the GitHub repository. This makes them available online for only you or the public to see depending on the privacy settings you set for your repo.
1. Type “git push”
2. Watch a long list of lines being displayed that will show the progress of the push operation.
git pull
This command is like the complete opposite of git push. It compares all the commits and if the local machine (i.e., your computer) is behind the repository, the commits are pulled, and the files are updated.
This makes working in a team easier as the progress can be updated and monitored very quickly without many hassles.
(As a good git practice, I would advice you to not work on the same file in the repository as some other team member. This may lead to a headache worse than getting a girlfriend — “merge conflicts”)
Approach 2 (VS Code)
1. Check you have Visual Studio Code installed and running.
2. Click on the “Open Folder” button and open the folder containing the repository.
3. Install Git Extension: In the Extensions view (Ctrl+Shift+X), search for Git related extensions. Popular ones include “GitLens” and “GitHub Pull Requests and Issues”. Install the one you prefer by clicking on the Install button next to it.
4. Commit Changes: In the Source Control view (Ctrl+Shift+G), you can see all the changes made to your files. You can directly commit all the changes by clicking on the Commit button. Add a commit message directly in the Source Control view. Just type your message in the textbox provided at the top and close the COMMIT_EDITMSG file
5. Push & Pull Changes: Once committed, you can push your changes or pull the latest version to the remote repository by clicking the Sync Changes button
Now, let’s explore another useful Git command:
git switch
This command is used to switch between branches in your Git repository. It allows you to navigate between different versions of your project’s codebase. Here’s how to use it:
- To switch to an existing branch, simply type git switch <branch name> in your terminal or Git Bash. You can also view the branch names by using “git branch” command
2. To create and switch to a new branch simultaneously, you can use git switch -c <new branch name>.
3. If you have uncommitted changes in your working directory, Git will prevent you from switching branches unless you either commit or stash those changes.
Understanding Git and GitHub is crucial for modern software development. Git provides version control capabilities, enabling teams to collaborate effectively and manage changes to their codebase. GitHub serves as a platform for hosting Git repositories, facilitating collaboration, code sharing, and project management.
By following the steps outlined in this blog post, you can set up Git, create a GitHub account, and start managing your projects with confidence. Whether you prefer the command line interface or the convenience of a graphical user interface like VS Code, Git offers powerful tools to streamline your development workflow.
Incorporating best practices such as using descriptive commit messages and avoiding merge conflicts will help ensure smooth collaboration within your team. So, dive into the world of Git and GitHub, and unlock the full potential of version control for your projects!