I have a confession to make, git intimidates me. There I said it! And it made me think, how can I overcome this fear? Flee? Fight? No. Instead, I’ve decided to take the time to get to know Git, and it turns out that it was all just a misunderstanding. Git is not so scary after all. Allow me to introduce you to each other and lets Git started!
What is Git?
So in simpler terms that we can all understand. It is a system that records changes to our files over time. It is mainly a unique tool that has a lot of advanced features for collaboration.
Disclaimer! You may have heard of Github, but it is not the same as Git. Github is a web service. It is kind of like a social network for software developers that can share code and collaborate on projects that are managed by Git.
Download Git here to get started.
Now let’s talk about repositories. Repositories are like directories for a project you want to track with Git. There are two types of repositories, local and remote.
Creating a Local Repository
To create a new repository with Git, open up a terminal, navigate to the main folder of your project, and type the following command.
Now that we’ve initialized a repository, the next command is git status.
This command is useful for checking the current status of your repository. As you can see here, I have no commits and three untracked files.
From here, we can choose what files we want Git to track. We can use the git add command to add our files to the staging area.
To add a specific file to the staging area, type the following command:
You will see now that the HTML file is green, indicating that it is staged and is ready to be committed.
To add multiple files, we can do this:
We can revert these changes as well:
For convenience, we can add all of the files in the current directory like this:
Now that our files are staged, we want to add them to a commit history of our repository. A commit is a term used for saving changes. When your files are committed, you can go back and check out these save points.
When calling git commit, it is required to include a message. The message should be a short description of the changes being committed. The message should be at the end of the command and it must be wrapped in quotations “ “
These are the two steps to committing. First, we add whichever files we choose to the staging area and then we commit. This way, you get the chance to customize what goes into a commit.
I’ve made some changes to my files and if I check the current status, you will see that they’ve been modified. Now we can stage and commit them individually if needed.
To see all the commits:
The logs will show a yellow text on the far left that represents the commit’s unique ID. You can use the generated ID to check out the changes you’ve made at some point in time. using the git checkout command:
git checkout <commit-id>
Replace <commit-id> with the actual ID for the specific commit that you want to visit.
To go back to the latest commit, you can type the command:
git checkout master
Branches are an essential part of Git. When you initialize a new repository, the master branch is created as well. The master branch represents the stable version of our code. So far, up until this point, all our files have been committed to this branch. You usually do not want to commit to the master branch because it is the official working version of your project. Instead, what you want to do is create a new branch to make changes before eventually rolling that branch back into the master.
To create an isolated branch:
git branch <new-branch-name>
Use the following command to view current branches in the code repository:
Switch to another branch:
git checkout <branch-name>
To create a new branch and switch to it at the same time, you can use the -b flag:
git checkout -b <new-branch-name>
Go back to the master branch:
git checkout master
Merge the changes from a different branch into your current branch:
git merge <branch-name>
You would replace <branch-name> with the branch that you want to integrate into your current branch.
Deleting a Branch
Delete a branch:
git branch -d <branch-name>
This was just a basic introduction to Git. If you still find Git intimidating, don’t worry, all you need is a little practice and soon enough, it’ll click. You just have to face it head-on. The unknown can be scary and that’s why you should get to know Git better.