GIT Tutorials

HOME

Git is a Distributed Version Control System (VCS) which is originally developed in 2005 by Linus Torvalds (Creator of Linux) and is open source, i.e. freely available to use. It is the most popular and most used version control tool right now. A staggering number of software projects rely on Git for version control, including commercial projects as well as open source. 

Chapter 1 How to install Git on Windows 10
Chapter 2 How to create a new Git Repository – git init Command
Chapter 3 How to stage changes in Git – git add Command
Chapter 4 How to unstage the changes in Git – git rm command
Chapter 5 How to commit changes in GIT – git commit command
Chapter 6 How to track commits in Git – git log command
Chapter 7 How to commit an empty folder in GIT – gitkeep
Chapter 8 How to ignore files in GIT – gitignore
Chapter 9 How to create a branch in GIT
Chapter 10 How to stash changes in GIT – git stash command
Chapter 11 How to push new local GIT Repository to GitLab
Advertisement

How to pull changes from Remote Repository to Local Repository – git pull

HOME

The previous tutorial has explained about pushing the changes from Local GIT Repository to Remote GIT Repository. This tutorial explains the process of pulling the changes from Remote Repository to Local GIT Repository. I’ll use GitLab to make the changes.

What is git pull?

The git pull command fetches and downloads content from the remote repository and integrates changes into the local repository. The git pull command is called as the combination of git fetch followed by git merge.

This is my project in GitLab. If, you don’t have any project in GitLab, follow the below steps to create a GIT local Repository and push it to GitLab.

Step 1– To create a new Git local Repository, use git init. The details are mentioned in this tutorial – How to create a new Git Repository – git init Command.

Step 2 – To create a new project in GitLab, refer to this tutorial – How to create a new project in GitLab.

Step 3 – To push your GIT local repository to Gitlab, refer to this tutorial – How to push new local GIT Repository to GitLab.

Step 4 – Let’s add a new file in the Remote Repository directly using GitLab. Follow the steps shown below to add and commit a new .

Step 5 – This is a new file. I have provided name to this file as products.txt. Add the comment as commit message. Click the Commit changes button to commit this change.

Below image shows that new file – products.txt is committed to this project.

Step 6 – Go to the directory where local GIT Repository is saved. Right-click and click on Git Bash Here to open GitBash at that place.

Use the below command to see the list of files present in GIT Local Repository.

ls -a
git log

Type git log to see the list of commits present.

Step 7 – Type git pull command is used to pull the latest changes from Remote GIT Repository to local GIT Repository.

git pull origin master

This command says that pull the content from the “master” branch in the “origin” repo.

As the “git pull” command fetches and merges, we can see the newly added file named “products.txt” in my local repo.

Again use the below commands:

ls -a
git log

This shows that the new file – products.txt which was present in GitLab is now present in Git local Repository means latest change is pulled from GitLab to local Repository.

Congratulation!! We are able to pull the changes from Remote Repository to Local Repository. Happy Learning!!

How to push new local GIT Repository to GitLab

HOME

This tutorial explains the steps that need to be followed to push a new local GIT Repository to GitLab.

What is GitLab?

GitLab is a single application that spans the entire software development lifecycle. GitLab is an open-source project maintained by GitLab Inc with over 3,000 contributors. We can install and self-manage the GitLab Community Edition which is fully open-source under an MIT license. GitLab also provides an Enterprise Edition that has additional features built upon the open-source edition. 

GitLab’s application offers functionality to collaboratively plan, build, secure, and deploy software as a complete DevOps Platform.

Steps to follow:

Step 1 – Login to GitLab using your username and password.

Step 2 – In the dashboard, click the blue New project button. This opens the New project page.

Step 3 – Select Create a blank project.

Step 4 – A new page will open. Provide the following information on that page:

1. Project Name – Mention the name of your project in the Project name field – GitTest.

2. Project slug – When a name is added, the Project slug auto-populates. This is the URL path for your project that the GitLab instance uses.

3. Project description (optional)  – This field enables you to enter a description for your project’s dashboard, which helps others understand what your project is about.

4. Visibility Level – Select the appropriate Visibility Level for your project. I have selected private option.

5. I’m not checking ReadMe option as I already have a ReadMe file in my project.

Select the Create Project button.

We can see that a new empty project is created in the GitLab as shown below.

Step 5 – For every remote repo, you will get a unique URL as highlighted below. This URL is used to push the local changes to this remote repo. Type the below command in GitBash:

git remote add origin https://gitlab.com/vibhasingh2004/gittest.git

To open GitBash, go to the location where the project is saved and Right Click and Select GitBash Here.

Add remote URL to local GIT Repository

git remote command provide the name to direct link to Repositories.

git remote command are used in conjunction with the git fetch, git push, and git pull commands.

git remote -v

This command list the remote connections we have to other repositories along with the URL of each connection.

Push the local changes to a remote repo

To push the changes from GIT Local Repository to GitLab in a particular branch of the remote repo, we need to use the below command:

git push <remote url or name> <branch name>

git push origin master

origin is the alias name of the full GIT Repository path (https://gitlab.com/vibhasingh2004/gittest.git)

Now run the Git push command as shown above. It will ask for your GitLab credentials in a new window as shown below. Please provide the username and password used to log in to GitLab.

Now we can see we have successfully pushed the local changes to the remote repository. Let’s go to GitLab and verify the latest changes. All the files from the local GIT Repository are moved to GitLab Remote Repository.

I hope this tutorial has helped you to set up a new project in GitLab and push the local changes to GIT Remote Repository.

How to stash changes in GIT – git stash command

HOME

The previous tutorial has explained about committing the changes in GIT. This tutorial explains about stashing the changes in GIT.

What is git stash?

git stash temporarily stash or shelves the changes made in working copy so that we can work on something else and later can apply back these changes.

Imagine a situation where you are working on a updating a feature, but suddenly there is a production bug on existing functionality of the feature which needs to be fixed immediately. So, we need to switch to bug fix and leave working on updation work. As the updation work is not completed, we can stash these changes and work on the bug fix. Later we can re-apply our stashed changes back to the code.

The git stash command can stash the uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. 

How to reapply stashed changes?

To re-apply the stashed changes, use this command

git stash pop

This command removes the changes from your stash and reapplies them to your working copy.

Alternatively, you can reapply the changes to your working copy and keep them in your stash with : 

git stash apply

Stashing untracked or ignored files

If a new file is created and not staged, then git stash is not going to stash that file.

Adding the -u option (or –include-untracked) tells git stash to also stash your untracked files:

git stash -u

Congratulation!! We have learnt about stash in GIT.

How to create a branch in GIT

HOME

The previous tutorial has explained the committing of the changes in the GIT. This tutorial explains the creation of a new branch in GIT.

Branching is a feature available in most modern version control systems. Git branches are effectively a pointer to a snapshot of the changes. When we want to add a new feature or fix a bug, it is advisable to create a new branch to encapsulate the changes.  This helps you to clean up the future’s history before merging it. Git branches are an essential part of the everyday workflow. Git does not copy files from one directory to another, it stores the branch as a reference to a commit.

A branch represents an independent line of development. The git branch command allows us to create a new branch, and rename and delete the branch.

It’s important to understand that branches are just pointers to commits. When you create a branch, all Git needs to do is create a new pointer, it doesn’t change the repository in any other way. To create a branch, use this command:

git checkout -b branchName

Let me explain how to create a new branch with the help of an example:-

Step 1– Create a new GIT Repository – GitTest.

Step 2 – Right-click and click on Git Bash Here to open GitBash at that place.

Step 3 – GIT Repo has already committed files – Index.txt, ReadMe.txt, .gitignore. Create a GIT local branch feature/customerDetails.

git checkout -b feature/customerDetails

Now, the branch is changed from master to feature/customerDetail.

Step 4 – Update the already committed Index.txt file. Later, type git add . to stage the file to GIT Staging area. Then, commit the Index.txt file.

echo -en "\n A new page for Customer is added." >>Index.txt
cat Index.txt
git status
git add.
git commit -m "Updated Index.txt file with Customer Detail"

Step 5 – Below command shows that the updated Index.txt is committed in new branch feature/customerDetails

git status

Step 6 – Type the below command to go back to the master branch.

git checkout master

Step 7 – Open Index.txt file in the master branch. It shows that there is no change in this file, as the changes were done in another new branch. To get the latest changes in the master branch, we need to merge the new local branch (feature/customerDetails) to the master branch.

To delete a branch, use the below command:

git branch -d feature/customerDetails

As the branch hasn’t been merged, the above command output an error message as shown in the above image.

To delete the branch forcefully, even if the changes are unmerged. Below is the command:

git branch -D feature/customerDetails

To rename the current branch, use the below command:

git branch -m feature/newCustomer

To get the list of all the branches, use the below command:

git branch -a

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

How to ignore files in GIT – gitignore

HOME

The previous tutorial has explained the addition of an empty file or directory to the GIT using .gitkeep. This tutorial explains how to not stage certain files in GIT (ignored file).

What type of files are ignored?

Ignored files are usually built artifacts and machine-generated files that can be derived from your repository source or should otherwise not be committed. Some common examples are:

  • dependency caches, such as the contents of /node_modules or /packages
  • compiled code, such as .o.pyc, and .class files
  • build output directories, such as /bin/out, or /target
  • files generated at runtime, such as .log.lock, or .tmp
  • hidden system files, such as .DS_Store or Thumbs.db
  • personal IDE config files, such as .idea/workspace.xml

Imagine we have 100 files, and we want to commit only 99 files and ignore 1 file. This seems to be a very difficult situation.

To add the files to the GIT Staging area, we can use git add command. If we use git add ., it will add all 100 files to the GIT Staging area. Another way is to mention git add file1, file2….. soon, which is also a very tedious task.

Ignored files are tracked in a special file named as .gitignore named which is checked in at the root of the repository.  We need to mention the file we want to ignore in .gitignore file.

Let me explain how to use .gitignore with the help of an example:-

Step 1 – Create a GIT Repository named as GitTest.

Step 2 – Right-click and click on Git Bash Here to open GitBash at that place.

Step 3 – Type git status to check if any file is not committed.

Step 4 – Create a new directory – config.

mkdir config

Step 5 – Create a file named test.class into the directory.

touch config/test.class

Step 6 – Type git status to check that config folder is untracked.

Step 7 – Create a .gitignore file.

touch .gitignore

Step 8 – Add the folder or file we want to ignore.

echo config/ >> .gitignore

Step 9 – Type git status again. This time, we can see that the untracked file is .gitignore not the config directory.

Congratulations!!! We are done. Have a happy Learning!!

How to commit an empty folder in GIT – gitkeep

HOME

In the previous tutorial, I have explained how to commit changes in GIT. This tutorial will explain how to commit an empty folder or directory in GIT.

GIT can not push empty directory or folder. It can only track files.

If we try to push a folder with nothing in it, although it will exist on our local machine, but nothing will go into the branch. A common, standardized practice to solve this issue is to push a file called .gitkeep into the empty folders. This is not a feature of GIT.

Let me explain this with an example

Step 1 – Create a GIT Repository with name GitTest.

Step 2 – Right-click and click on Git Bash Here to open GitBash at that place.

Step 3 – Create an empty directory named as login.

mkdir login

Step 4 – Type git status to check if new directory is tracked by Git or not. This image shows that GIT didn’t track the new directory login.

Step 5 – Create a new empty file called .gitkeep into the login directory.

touch login/.gitkeep

Step 6 – Type git status again and now the new directory login is tracked by GIT.

I hope this tutorial has helped you to understand this concept. Have a nice learning!!!

How to track commits in Git – git log command

HOME

The previous tutorial explains the process of staging and committing a change. But, to track all the commits that have occurred in the project, there is a command called git log.

What is git log command?

The git log command shows the snapshot of all the committed changes.  It is used for listing and filtering the project history, and searching for particular changes.

To explain the usage of the git log, let us follow a few steps.

Step 1 – Create an empty directory – GitTest.

Step 2 – Right-click and click on Git Bash Here to open GitBash at that place.

Step 3 – Type a git command as shown below. This command is used for the initial setup of a Git local Repository. To know how to create an empty directory, refer to this tutorial.

git init

Step 4 – Use the below command to display the state of the working directory and the staging area.

git status

Step 5 – Create 2 new files with some content – ReadMe.txt and Index.txt.

Step 6 – Use the below command to promote pending changes (new file or altered file) in the working directory, to the git Staging area.

git add ReadMe.txt

Step 7 – Use the below command to promote the changes from Staging Area to GIT local repository. This is our first commit.

git commit ReadMe.txt

Step 8 – Update ReadMe.txt file

Step 9 – Use the below command to promote both ReadMe.txt and Index.txt to the staging area, which means both files are staged.

git add .

Step 10 – Use the below command to commit both the files to GIT local Repository.

git commit -am "Second Commit with Index and ReadMe files"

Step 11 – Type the below command to see the snapshot of all the commits.

git log

By default, formatting, shows the whole commit history. 

There are various configurations of git log:-

1. git log –patch or git log -p

This command shows the patch for each commit as well as their full differences.

2. git log –stat

This command displays the number of insertions and deletions to each file altered by each commit (note that modifying a line is represented as 1 insertion and 1 deletion). This is useful when you want a brief summary of the changes introduced by each commit. 

3. git log –pretty

git log --pretty=format:"%cn committed %h on %cd"

This command display each commit using printf-style placeholders. The %cn%h and %cd characters in the command are replaced with the committer name, abbreviated commit hash, and the committer date, respectively.

4. git log -n where n is the limit

git log -1

This command limits the number of commits to be displayed.

5. git log –author

git log --author ="Singh"

This command displays all commits for the specified author. If there is no match for the author, nothing will be displayed.

6. git log — fileName

git log -- Index.txt

This command displays those commits that include the specified file, which makes it easier to see the file’s history.

7. git log –after

git log --after="yesterday"
git log --after="2022-02-15"

This command displays a commit from a specific time frame. We can use both –after or –before.

Congratulations!! We have learned about the git log, which is very useful for tracking the changes. Happy Learning!!

How to commit changes in GIT – git commit command

HOME

The previous tutorial explains the staging of the changes. This tutorial explains how to commit changes in GIT.

As we remember from previous tutorials, committing a change (like new file, updated file, etc) is a 2 step process.

“git add” is the first command in a chain of operations that directs Git to “save” a snapshot of the current project state, into the commit history or can say . Now we can say that “git commit” is the second command in above chain of operations.

git commit is used to save the changes to the local Git Repository. When a file is changes and we use git commit, GIT will not automatically commit the changes. Instead, we need to use the “git add” command to mark the desired changes for inclusion.

Let me explain this with an example.

Step 1– Create an empty directory – GitTest.

Step 2Right-click and click on Git Bash Here to open GitBash at that place.

Step 3 – Type a git command as “git init”. This command is used for initial setup of a Git local Repository. To know how to create an empty directory, refer this tutorial.

Step 4 – Type git status command. This command displays the state of the working directory and the staging area.

Step 5 – Create 2 new files with some content – ReadMe.txt and Index.txt.

Step 6 – Type git add ReadMe.txt command. This will  promote pending changes (new file or altered file) in the working directory , to the git Staging area.

Step 7 – Type git status command.

This shows that ReadMe.txt file is already staged and Indesx.txt is not staged. As ReadMe.txt is already staged, we can commit this file to Git local Repository. This can be done by using the command “git commit”.

Step 8 – Type git commit “ReadMe.txt”.

git commit <fileName>

This will launch a text editor prompting you for a commit message. After you’ve entered a message, save the file and close the editor to create the actual commit. Below is the editor opens. I have added the message – First commit at line 15 (which is not commented – #)

This is how the message is displayed in GitBash now.

Step 9 – Type git status again. This command shows that now we have only Index.txt to stage and commit.

A shortcut command that immediately creates a commit with a passed commit message without opening of text editor.

git commit -m "commit message"

There are some other options to available with git commit.

  1. This command commit a snapshot of all changes present in Git Staging area.
git commit -a

2. A power user shortcut command that combines the -a and -m options. This combination immediately creates a commit of all the staged changes and takes an inline commit message.

git commit -am "commit message"

In the below example,

Step 1 – Modified Index.txt file

Step 2 – Type git status to see that ReadMe.txt is modified so needs to be staged again and Index.txt which also needs to be staged.

Step 3 – Type git add . to stage both files Index.txt and ReadMe.txt to Staging Area.

Step 4 – Type git status to check the status of both files.

Step 5 – Type git commit -am “Second commit with Index and ReadMe files” to commit both the files.

Step 6 – Type git status to check if both the files are committed or not.

Hopefully, this tutorial will help you to understand the process of committing the changes in GIT. Happy Learning.

How to unstage the changes in Git – git rm command

HOME

The previous example has explained the staging of new files to the staging area.

What is git rm?

This command removes the file from the staging area. The files from the working directory will remain intact. This means that you’ll still have a copy of the file locally. The file will be removed from the index tracking of Git project.

Let us explain this with the help of an example.

  1. Create an empty Git RepositoryGitTest
  2. Create a new file – ReadMe.txt and add the file to staging area.
  3. Create another file – Index.html
  4. Update the first file – ReadMe.txt which is already staged.
  5. Use “git add .” to stage both the files.

Later, we realized that we don’t want to commit both the files. We try to commit the logically same files, like files created or changed to fix a particular bug. So, if in the future we want to track back the changes, it is easy to figure out which all files are changed or impacted.

We want to unstage the “Index.txt” file.

Use the below command to reset the changes from the staging area to the working directory.

git rm --cached Index.txt

The “cached” option specifies that the removal should happen only on the staging index.

I have used “git status” command and it shows that “ReadMe.txt” can either be unstage or commit. Whereas “Index.txt” is unstage. So, if we want to commit Index.txt, first we need to stage this file using git add command.

This image shows that git rm moves the staged file from Staging Area to back to working Directory.

I hope this tutorial has helped you to understand git rm command.