DEV Community

John Mutisya
John Mutisya

Posted on

Lets Take A Dive Into Git

What is Git?

Git is an open source distributed version control system. To break it down;

Git is a control System: It’s a content tracker. It is used to store content especially code due to the features it provides.
It is a version control system: Code that is stored in Git keeps on changing as developers keep on making changes to the existing code and even adding new code. Git serves as a version control system by handling these changes and maintaining a history of those changes as they happen.

It serves as a distributed version control system in that, it has a remote repository stored in a server and a local repository stored in the computer of each developer. Each developer’s computer has the full copy of the code and thus distributed.

Installing Git:

Git is an open source application and it can be downloaded online for free. This link, Will take you to their website from where you can download Git for your specific platform.
You’ll also find documentation on how to install it.
You can verify if Git has been installed successfully by running this command in the command prompt.
Git –version

Below are the steps of working with Git to make changes to a code base, opening a pull request(PR), and merging code into the primary branch.

Creating a Local Repository

A repository is a directory that stores all of the files and folders needed for a project and the changes made to them.

When creating a new project on your local machine using, you’ll first have to create a new repository often referred to as ‘repo’.

These commands are used to change a directory and create a new one;
Cd ~/desktop
Mkdir gitdemo

To initialize a repository in the root folder we will use,
Git –init in the command terminal.

Adding a new file to the repository.

We can add a new file to the project using any text editor or by running the touch command. For example touch newfile.txt, this creates and saves a blank file named “newfile.txt”.

Once we have added or modified files in a folder containing a git repo, git will notice that the file exists inside the repo, but won’t track the file unless you tell it to explicitly.

Git only saves or manages changes to files that it tracks. For it saves the new changes we need to send it a command that tell it that we want to track our new file.

touch newfile.txt

We can use the git status command to see which files git knows exist.

BERNARD@BERNARD-PC MINGW64 ~/desktop/gitdemo (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

nothing added to commit but untracked files present (use "git add" to track)

BERNARD@BERNARD-PC MINGW64 ~/desktop/gitdemo (master)
Enter fullscreen mode Exit fullscreen mode

Staging and Committing the code

Committing is the process of adding code to the local repository. Before committing the code, it has to be in the staging area. The staging area is there to keep track of all the files which are to be committed.

Files that have not been added to the staging cannot be committed.


To add the file to the staging area we will use the following command:

git add newfile.txt

Incase you want to add muiltiple files you can run the command as shown below,

git add file1 file2 file3

To add the files inside your projects folder into the staging area we use:

Git add .

Use the above command with caution as it adds all the files and folders in your project to the staging area.

To commit the file we will use:

Git commit –m “Message describing the commit”.

The message in the double quotation marks is the commit message. Make sure to use a relevant commit message that describes what code changes were done in that particular commit.
Create a new branch

Now that you've made a new commit, let's create a branch.
Say you want to make a new feature but are worried about making changes to the main project while developing the feature. This is where git branches come in.

Branches allow you to move back and forth between 'states' of a project. Official git docs describe branches this way: ‘A branch in Git is simply a lightweight movable pointer to one of these commits.’ For instance, if you want to add a new page to your website you can create a new branch just for that page without affecting the main part of the project. Once you're done with the page, you can merge your changes from your branch into the primary branch. When you create a new branch, Git keeps track of which commit your branch 'branched' off of, so it knows the history behind all the files.

Let's say you are on the primary branch and want to create a new branch to develop your web page. Here's what you'll do: Run git checkout -b <my branch name>. This command will automatically create a new branch and then 'check you out' on it, meaning git will move you to that branch, off of the primary branch.
After running the above command, you can use the git branch command to confirm that your branch was created:

git branch

Create a new repository on GitHub

If you only want to keep track of your code locally, you don't need to use GitHub. But if you want to work with a team, you can use GitHub to collaboratively modify the project's code.
To create a new repo on GitHub, log in and go to the GitHub home page. You can find the “New repository” option under the “+” sign next to your profile picture, in the top right corner of the navbar:

After clicking the button, GitHub will ask you to name your repo and provide a brief description.

When you're done filling out the information, press the 'Create repository' button to make your new repo.

GitHub will ask if you want to create a new repo from scratch or if you want to add a repo you have created locally. In this case, since we've already created a new repo locally, we want to push that onto GitHub so follow the '....or push an existing repository from the command line' section:

git remote add origin
git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 263 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
 * [new branch]      master -> master
Enter fullscreen mode Exit fullscreen mode

Branch master set up to track remote branch master from origin.
(You'll want to change the URL in the first command line to what GitHub lists in this section since your GitHub username and repo name are different.)

Push a branch to GitHub
Now we'll push the commit in your branch to your new GitHub repo.

This allows other people to see the changes you've made. If they're approved by the repository's owner, the changes can then be merged into the primary branch.

To push changes onto a new branch on GitHub, you'll want to run git push origin yourbranchname. GitHub will automatically create the branch for you on the remote repository:

git push origin my-new-branch
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 313 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
 * [new branch]      my-new-branch -> my-new-branch
Enter fullscreen mode Exit fullscreen mode

You might be wondering what that "origin" word means in the command above. What happens is that when you clone a remote repository to your local machine, git creates an alias for you.

In nearly all cases this alias is called "origin." It's essentially shorthand for the remote repository's URL. So, to push your changes to the remote repository, you could've used either the command: git push yourbranchname or git push origin yourbranchname
(If this is your first time using GitHub locally, it might prompt you to log in with your GitHub username and password.)

If you refresh the GitHub page, you'll see note saying a branch with your name has just been pushed into the repository. You can also click the 'branches' link to see your branch listed there.

Create a pull request (PR)

A pull request (or PR) is a way to alert a repo's owners 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 primary branch.

You might see a big green button at the bottom that says 'Merge pull request'. Clicking this means you'll merge your changes into the primary branch..

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.

Merge a PR

Go ahead and click the green 'Merge pull request' button. This will merge your changes into the primary branch.

Get changes on GitHub back to your computer

Right now, the repo on GitHub looks a little different than what you have on your local machine. For example, the commit you made in your branch and merged into the primary branch doesn't exist in the primary branch on your local machine.

In order to get the most recent changes that you or others have merged on GitHub, use the git pull origin master command (when working on the primary branch). In most cases, this can be shortened to “git pull”.

git pull origin master
remote: Counting objects: 1, done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), done.
 * branch            master     -> FETCH_HEAD
   b345d9a..5381b7c  master     -> origin/master
Merge made by the 'recursive' strategy.
newfile.txt | 1 +
 1 file changed, 1 insertion(+)
Enter fullscreen mode Exit fullscreen mode

This shows you all the files that have changed and how they've changed.

Top comments (0)