DEV Community

Cover image for How to learn Git - a guide for beginners
Abu Nayeem Tasneem
Abu Nayeem Tasneem

Posted on • Updated on

How to learn Git - a guide for beginners

Git is an open source version control system. In simple words it keeps track of changes in files by a single person or a group of people. In this post you will learn what is Git and How to manage your projects with Git.

Terminologies

While learning git, you will come across some terms which may be new to you. Let's start with learning what the most essential terms mean.

Repository: This is where everything starts. Repository is a data structure, but let's see it in simpler way. It's a directory where the changes in your project is tracked and stored. You have to declare the parent folder of your project as a repository. Then Git will create a directory named .git in there and store all the changes in it. Thus you have turned the parent folder of your project into a Git Repository.

Local Repository: The version of project in your local computer.

Now let's dive into how to start working with Git. Some of the important terms are explained below for reference.

Git Workflow

For windows users, you can install git from here. For linux users, it's highly likely you already have git installed in your system. If not, you can easily use the package manager of your respective distro to install git.

Git is generally used from the terminal. So in windows you have to either use the outdated Command Prompt (CMD) or you can also use powershell. You have to open your terminal and type the command. Git commands follow this simple structure:

git your-command
Enter fullscreen mode Exit fullscreen mode

You can create your first repository following this workflow. Let's dive into a very basic git workflow:

  • Initiate / clone a repository: First you need a repository to work on. You can clone one or create one in your local machine. To initiate a repository in your project folder:
cd <your-project-folder>
git init
Enter fullscreen mode Exit fullscreen mode

To clone a remote repository:

git clone <url-of-remote-repository>
Enter fullscreen mode Exit fullscreen mode
  • Create Branch / Make changes: You can start working on your project or create a branch of an existing project for experimenting. To create branch:
git branch <name-of-your-branch>
Enter fullscreen mode Exit fullscreen mode
  • Stage the changes: Add all files in current directory to be taken snapshot as a version.
git add * 
Enter fullscreen mode Exit fullscreen mode
  • Commit the changes: Take a snapshot of the project in it's current stage.
git commit -m <message> 
Enter fullscreen mode Exit fullscreen mode

Now if you choose you can make this snapshot the main version of your project or keep it as your experimental branch. Every commit has it's unique SHA1 identifier. Following command shows commit history and their details:

git log 
Enter fullscreen mode Exit fullscreen mode
  • Push The changes: Push the latest snapshot to the remote repository.
git push / git push <name-of-your-branch> 
Enter fullscreen mode Exit fullscreen mode
  • Checkout: You can use checkout to navigate to a certain branch and work on it.
git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Suggestions

That's a wrap. Thanks a lot for reading. Hope I could've helped you start learning git. If you have any suggestion about the blog post please let me know in the comments.

Check out some more resources on git:

Official Git Book
Git Tutorial - Atlassian
Git Tutorial - Hubspot

Appendix:

Remote Repository: To keep backup of your repository, and also to allow others to make changes and do a lot of other things, you can keep your repository on a public/private server. This is remote repository. Services like GitHub / GitLab let's you keep repositories on their server for free.

Clone: Cloning a repository means getting an exact copy of the remote repository in your local computer.

Branch: A Branch of the main project.

Fork: A Cloned version of the main project in which you can run experiments without affecting the main version. This version is generally used by contributors.

Staging: After you have made changes in your project files you have to add them to the staging area. It's a list of files to track changes. These changes will be added in the next snapshot.

Commit: Create a snapshot of the project in it's current stage. Git uses snapshots of a project to keep track of different versions.

Push: Upload contents of local repository or a particular commit(snapshot) to remote repository.

Git vs GitHub: If you have any confusion regarding Git vs GitHub, Git is a version control system. Which you can use in your local system as well. Whereas GitHub is a service that let's you keep your git repositories online.

Cover Photo Source

Top comments (0)