This is the second article in the series — ‘Git For Beginners’. In the first article, I’ve explained the basic Git terminologies and in this, we will follow up with creating a basic Git repository and working on it. We will learn how to create a repository, clone it in our local machine, make a commit, push/pull changes, creating branches, and check Git status & logs.

I’ve covered Git in 3 parts of Git For Beginners series —

  1. Basic Git Concepts.
  2. Working With Basic Git Commands (You are here).
  3. Working With Advanced Git Commands.

We are using GitHub where we will be creating a repository and executing the Git commands. Create an account in GitHub so you can follow with the remaining flow.


Creating A Git Repository

To get started, we will first need to create a repository. Go to GitHub and click on ‘New’ next to the repository. Enter the repository name and select it to be either Public/Private. Check the ‘Initialize this repository with a README’ option and click on ‘Create Repository’. Once you do that, a new repository will be created.

Setting Up Git In Local Machine

To work with Git commands in our local machine, we will have to set up Git first. For doing so, we will need to install Git in our machine and configure the credentials. Atlassian has written a nice article for doing so — https://www.atlassian.com/git/tutorials/install-git. Once done, you will be able to execute Git commands in your console window directly.

Cloning A Git Repository

Once we are done with creating a Git repository, we will clone and make it available in our local machine. For doing that, click on the ‘Clone or download’ dropdown option and copy the repository URL using the clipboard icon present in the dialog box.

Cloning a Git repository
Cloning a Git repository

Once done, open console and navigate to the location where you would like to clone and make the repository available in your local. Use —

git clone {copied repository url}
Cloned repository available in local machine
Cloned Git repository available in local machine

Commit Changes To Git

Once we have the repository available, we will want to work on it. We would either add new files or edit the existing files and would want the changes to be reflected in the remote repository so that the changes are visible and available for other contributors too.

Let’s now add a new file ‘index.html’ in the repository folder and we will commit it. But before committing the file, we will check if Git is able to detect the newly added file. To do so, we will run in our repository directory —

git status

It will show us the untracked files list and also our newly added ‘index.html’ file is present there.

Git Status — Track change status
Git Status — Track change status

So, now we know that Git is recognizing our file. We will have to add the changes done to the file in the Git so that it can be committed. For that, we have the command — git add

We can use git add in two ways —

  1. Add all the file changes to the commit.
  2. Add specific file changes to the commit.

One question we would get is what do the two options do and when to use them —

We would use the first option when we want to commit all the file changes we have done and we would go with the second option when we want to commit only specific file changes. The second option might not seem relevant or useful initially because we will be either individuals or a very small team working on the project and we would commit all our works but in complex products, for running the project in our local machine we would be doing some changes which we would not like to commit or we might work on multiple changes but would want to have the changes in different commits. In that case, we can add the files individually.

Git Add

To use Git Add, we would execute —

1. git add .
2. git add {filename}
Add — track changes done in the working directory
Git Add — track changes done in the working directory

Git Reset

If you have added a file by mistake and want to untrack it, you can do that easily by using the command —

git reset HEAD {file_name}

For discarding the changes in a file, we will have to first untrack it by using the above command and then execute the following command —

Git Checkout

git checkout -- {file_name}

For removing multiple files we can do it as —

git checkout -- {file1} {file2}
(or)
git checkout -- filename*

Git Commit

Now that we have the files ready to commit, we will commit it using the command —

git commit -m "Commit Message"

-m is used to append a commit message directly. Commit Message is very important as later when we have 100s of commits, we will be able to read the commit message and know what changes were done in the commit easily. If by mistake you have added the wrong message, you can update it easily by using the following command —

git commit --amend

Now if we run the ‘git status’ command, it will show us nothing to commit. We can verify if the commit was added by running —

Git Log

git log

Git log helps us to check the commit history. We will notice here that a new commit with a Commit ID and our specified Commit message was added here. We can add ‘–oneline’ tag to view the Commit ID and Message in a single line. To list the last ‘n’ commits, we can use ‘-n {n}’ tag, where, ‘n’ is the last n commits we want to list.

commit — To commit changes | log — To view commit history
git commit — To commit changes | git log — To view commit history

Another useful command to know here is —

git reflog

reflog command is useful to know when the HEAD pointer was switched between branches. Basically it shows when the git refs were updated in the local repository. Certain Git commands accept a parameter for specifying the reference. You can read more about Git Reflog — here.

So we have committed our changes and it is visible in git log but if we open our git repository in remote, we will not see the changes there. So once we commit, it is done in our local only. We will need to push the changes to make it available in our remote also.

Pushing Local Commits To Remote

Now that we have the commit done in our local, we can push the changes to the remote repository so that it becomes available to others. For doing so, we can execute the command —

git push

If for the first time you get an error such as remote URL not set, you need to add the repository URL and associate it with a name (commonly ‘origin’ is preferred).

git remote add {repository_name_for_local_purpose} {repository_url}

Example — git remote add origin https://github.com/varundwarkani/demo-repository.

Once the command is executed, in the console you would see something like this —

git push command
git push command

This means that the push was successful. Now if we open our repository, we will be able to see the changes and commit available in the remote repository as well.

push — remote repository
git push — remote repository

Chaning Default Name And Email ID

For some reason, we might want to commit changes using a different name or email id. We can do that by changing the configuration in Git. For changing the configuration globally, execute the following command in Console —

git config --global user.name "User_Name"
git config --global user.email "your_email@example.com"

You can see the configuration by running the command —

git config --list

If you want to change it only for a specific repository, you can do so by executing the following command in the root folder of the repository —

git config user.name "User_Name"
git config user.email "your_email@example.com"

Another way is to navigate to the .git/ directory and editing the config file. In the config file, we will add the following lines —

[user]
     name = YOUR_USER_NAME
     email = YOUR_EMAIL

Pull / Fetch Changes From Git

While we are working on the project and if there are new commits available in the branch, we can pull those changes and sync our local repository with the remote so that we have the latest code available in our local repository too. And, before we can push our changes, if there are commits in the remote then we will have to pull the changes before we are allowed to push our changes.

Git Fetch is used to seeing if there are any commits available to be pulled from the remote repository. One important thing to know here is that Git Fetch does not do any file transfer rather it takes the meta-data and shows the difference.

git fetch — Command to fetch changes
git fetch — Command to fetch changes

Git Diff is an interesting command which helps in viewing the difference between the commits/branches/files and others. Here we have used it to view the changes available in the remote.

Git Pull on the other hand checks if there are any changes available in the remote repository AND does the file transfer and updates the files locally.

git pull — Command to pull changes
git pull — Command to pull changes

It is a bit safer to use git fetch first to know what changes are available and then do git pull to fetch those changes.

Pushing Existing Project To Git

Many times we would have started working on a project and then it would occur to us that we can have the code in Git for either making it open-source or sharing with other contributors. We have two options here. Either we can clone a Git repository, copy-paste our code into that repository, push the changes, and continue with our work. Or we can connect the existing project to a Git repository.

For connecting an existing project to a Git repository, first, we would need to create a Git repository. In the console, navigate to the project folder and execute the following commands —

git init

This command will initialize a Git repository in the project folder.

Now, we will add the files using git add and commit it. After this, we will connect our git repository to the local project folder and push the changes —

git remote add origin {repository_url}
git init — Command to initialize Git in a local project
git init — Command to initialize Git in a local project

Now push the changes to Git and the new files will be available in remote Git repository —

Local project folder available in the remote repository
Local project folder available in the remote repository

Working With Branches In Git

You would’ve noticed the term ‘master’ in various places. The master here refers to Branch we are working on. So what is a branch in Git? “Branch is a lightweight movable pointer to one of the commits”. Initially, Git provides us with a master branch to work on. As and when we add commits, the master branch pointer moves and points to the latest commit (each commit has a unique Commit ID associated with it).

More about Branches and Git Flow is mentioned in the Part-I of the series. Take a look here if you haven’t read it yet.

First, we would create a new branch. The below command will help us in doing so —

git checkout -b "Branch Name"

git checkout command is used to switch between branches in a Git repository. Here, -b is used to create a new branch and checkout that branch. After we create, we need to push the changes so that the branch is available in the remote repository too.

git branch — Creating a new git branch
git branch — Creating a new git branch
Development branch available in the remote repository
Development branch available in the remote repository

You can easily switch between the branches by using —

git checkout {branch_name}

If you wish to delete a branch, we can do it in two ways —

  1. Delete it locally.
  2. Delete it from local and remote repository.

For deleting, first, we will have to checkout a different branch. We do this for the same reason that we cannot sit on a branch and cut it from the tree.

For doing it locally, we can use the command —

git branch -d {branch_name}
(or)
git branch -D {branch_name}

Here, -d is an alias for –delete and -D is for force delete. But if you do this, the branch will still be available in the remote repository. To delete the branch from remote, we can use the command —

git push -d origin {branch_name}
branch -d | push -d — Deleting a git branch
git branch -d | git push -d — Deleting a git branch

Git Patch

The last concept in the basic commands is Git Patch. Git Patch is a very useful concept that can help you to apply your code changes on any branch easily. It creates a git diff of the changes which also includes metadata about commits and can be applied directly on any branch.

For creating Git Patch, the command is —

git format-patch -{n} {commit_id}

Here, {n} is the number of commits to be included, and {commit_id} is the SHA of a commit but this parameter is optional.

git format-patch — Command to create a patch
git format-patch — Command to create a patch

For applying the patch, there are two ways. The commands are —

git apply {patch_file_name}

Git Apply applies the patch changes without creating any new commit.

For applying specific files from a patch, we can do that as —

git apply --include={file_name} {patch_file_name}

Another method is,

git am < {patch_file_name}

Git Am applies the patch changes and creates a new commit.

git apply patch — Command to apply the patch
git apply patch — Command to apply the patch

Final Words

We have learned a lot of Git commands and have understood the basic flow too. It might seem difficult at first but as but when we try and keep using it in our lives, it will be at our fingertips easily. The Part-III in the series which will give more idea on merging commits, stashing, rebasing, resetting commits, and much more. Do give it a read — here.

Leave a Reply

Your email address will not be published. Required fields are marked *