DEV Community

Cover image for Learning Git 101
Prashant Pal
Prashant Pal

Posted on

Learning Git 101

πŸ“š In my recent blogs, I've shared my Linux learnings so far. 🌐 Although I haven't yet written about Linux networking, I’ll soon compile what I’ve learned in that area. Today’s topic, however, is version control! πŸ”„ I’ll be focusing on GitLab, which is similar to GitHub, and I’ll share everything I’ve learned about using it. πŸš€

Why There Is a Need for Git? πŸ€”

While working on a project different developers work on the same code πŸ§‘β€πŸ’»πŸ‘©β€πŸ’». They have different roles in that project, like frontend, backend, testing, etc. πŸ–₯οΈπŸ” And we need to share the same code with different developers. So, how do we share the code with each other? πŸ€·β€β™‚οΈ

Here comes Git! πŸš€ In Git code is hosted centrally on the internet in a code repository πŸ“‚πŸŒ. Now every developer has an entire copy of the code locally πŸ’Ύ. Code is fetched from the remote repository πŸ“₯ and pushed to the code repository πŸ“€. Git knows how to merge changes automatically πŸ€–.

But there’s a problem when the same line is changed by two developers πŸ›‘, Git can’t fix it, and it causes merge conflicts ⚠️. The best practice is to keep pushing and pulling to the repository πŸ”„. Version control keeps a history of changes πŸ“œ, and you can revert the commits you made βͺ.

Basic Concepts of Git πŸ› οΈ

Git is the most popular version control tool 🌟. Here are some key concepts you should know:

  1. Remote Git Repository 🌐: These are centrally hosted repositories. You can manage them using various version control tools like GitHub, GitLab, Bitbucket, etc. πŸ–₯️ It can be public or private πŸ”.
  2. Local Git Repository πŸ’»: These repositories are hosted on your system.
  3. History of Code πŸ“œ: You can view your Git history by typing the command git log.
  4. Staging πŸ“: This is the area where you decide which changes to commit. Staging prepares the code for committing βœ….
  5. Git Clients πŸ”§: These tools help developers interact with Git repositories.

Note πŸ“: Companies often use their own Git servers, like Bitbucket.

How to Set Up a Repository πŸ› οΈ

There are different version control tools available 🌐. First, create an account on any platform. Here, I use GitLab, which is similar to GitHub πŸ–₯️.

GitLab Dashboard

After making an account πŸ“, we need to connect the Git client with the remote platform 🌐. This involves authenticating GitLab to enable pushing changes from the local repository πŸ’» to the remote repository πŸ“‚. For this, we can set it up by adding an SSH key πŸ”‘.

creation of SSH key

After doing this πŸ”‘, GitLab can authenticate us when pushing or pulling from the repository πŸ”„. You can check the next steps to clone the repository πŸ“‚β¬‡οΈ.

Work with Git ✨

There are mainly 3 stages πŸ› οΈ:

  1. Working directory πŸ“: Where new files are created.
  2. Staging area πŸ“‚: This represents that files are ready to push. Use git add to get the file in the staging area so it can be committed.
  3. Local repository πŸ’Ύ: Here you can commit the changes. This is your system on which you are working.

I am creating a basic file to show how we work with Git. I create a README.md file πŸ“„. After creating, type the command git status. This command shows the current status of the local Git repository πŸ“Š.

After that, we use the command git add . to move the file to the staging area. Then, type the git status command. It will show the file in green color βœ…, which means that the file is in the staging area. Now it can be tracked.

It's time to commit the changes πŸŽ‰. Use the command git commit. Here is the screenshot of the commands I have used πŸ“Έ.

GIt status

Now it is time to push the work to remote repository that is on GitLab. Simply just write the command git push in the terminal it will do the work.

Note:- If you stuck and getting error I suggest use StackOverFlow or use ChatGpt to resolve the issue.

Initializing a Repo πŸ› οΈ

If you have worked on your project locally and you want to push it to your GitLab account πŸ™, there is a way to make that happen.
You need to make that folder a Git repository πŸ“. Use the command git init to initialize a Git repo. Now follow the same steps above.

But you might notice there is an error while pushing the code 🚨. This happens because there is no destination defined. To fix this, create a repository on your GitLab account πŸ›‘οΈ. After creating it, you will see a section "Push an existing folder" πŸ“‹. From there, copy the git remote add command and paste it into your terminal πŸ’».

However, you might still get an error related to the master branch ⚠️. To resolve this, use the command showing in your terminal: git push --set-upstream origin master πŸ”—. Now the branch is connected too βœ….

Branching Concepts 🌿

The Master Branch πŸ› οΈ is known as the main branch. It is created by default when you initialize a Git repository. New features, bug fixes, and stacks of changes in the master branch help divide the work among developers by creating a branch for each feature ⚑.

How to create a new branch? πŸ€”
The best practice is one branch per bugfix or feature πŸ›βœ¨. Developers can commit without worrying about breaking the main branch πŸš€. Once changes are complete, merge the branch πŸ›‘οΈ. Then, a new version can be released πŸŽ‰.

However, large feature branches πŸ“ that stay open for too long increase the chance of merge conflicts ⚠️. By having separate branches, the main branch remains stable βœ….

The git pull command is used to refresh new changes in the remote repository πŸ”„. To switch between branches, use the command git checkout <branch name> πŸ”€.

To create new branches in the CLI πŸ–₯️, first type git checkout master to switch to the master branch. Then, create a new branch by using the command git checkout -b <branch name> πŸ†•. This command will create a new branch and automatically switch you to it πŸš€.

However, this branch exists locally only πŸ—‚οΈ and does not replicate in the remote repository 🌐. To sync it with the remote, make changes, commit those changes, and use the command:
git push --set-upstream origin <branch name> πŸ”—.

Note πŸ“: In a project, there are typically two branches:
Master branch πŸ› οΈ (main branch).
Develop branch βš™οΈ (ready for production).

Merge Requests πŸ”„

Merge requests are typically done by other developers πŸ‘¨β€πŸ’»πŸ‘©β€πŸ’». Afterward, experienced team members πŸ‘“ review the code before merging the changes.

This process ensures that the master branch πŸ› οΈ remains stable βœ….

Deleting Branches πŸ—‘οΈ

After completing work on a branch, it’s a good practice to delete it 🧹. Branches can be deleted in two ways:

  1. Through the remote repository 🌐:
    Simply go to the branches section in your account and delete the branch πŸ–±οΈ.

  2. From the local environment πŸ–₯️:

First, switch to the master branch and pull the latest changes:
git checkout master && git pull πŸ”„.
Then, delete the branch locally using the command:
git branch -d <branch name> πŸ—‚οΈ.

Rebase πŸ”„

Imagine you’re working in a bug-fix branch πŸ› οΈ and have made some changes. At the same time, another developer also makes changes in the same branch πŸ”€ or in the remote repository 🌐. The changes made in the remote repository won't be reflected in the local repository of the first developer and vice versa.

In this scenario, Git will show that there are some changes. First, you need to pull those changes ⬇️ and then push your own changes ⬆️. This could result in multiple commits being pushed, which is not a best practice ⚠️.

To solve this, use the command:
git pull -rπŸ”—.
This command rebases the changes, stacking your commits neatly on top of the latest remote changes πŸ“š.

.gitignore File πŸ“„

When you work on a project in your local environment πŸ–₯️, certain files are generated, such as dependencies, build files, and other temporary files βš™οΈ. To exclude these files or folders from being tracked by Git πŸ› οΈβ€”especially those specific to your editorβ€”you can use a .gitignore file 🚫.

For example, in build folders where compiled code is located πŸ—οΈ, you can create a .gitignore file and list those files or folders to prevent them from being tracked. This keeps your repository clean and focused on important files βœ….

./idea/*
/build/*
/node_modules/* 
Enter fullscreen mode Exit fullscreen mode

To stop tracking a file use command git rm --cached <folder name>.
To revert commits use command:-

git reset --soft Head~1
git commit --amend
git push --force
git revert <commit id>
Enter fullscreen mode Exit fullscreen mode

And git merge πŸ”„ merge the changes from the master branch πŸ› οΈ to another branch πŸ”€, make sure that your local master branch πŸ–₯️ is up-to-date βœ… for that use git checkout master, git pull, go back to your branch πŸ” git merge master πŸ”—.

Resources πŸ“šπŸŒπŸ’‘

  1. How to make ssh keys Link
  2. Git Cheat Sheet Link

Conclude βœ…πŸ”šβœ¨

In this blog, I’ve covered the basics of Git πŸ§‘β€πŸ’», but there are still plenty of concepts I couldn't cover πŸ“š. Git is a vast tool with much more to explore. In my next blog, I will dive into build tools πŸ”§, starting with npm for JavaScript and Maven and Gradle for Java applications β˜•. Stay tuned for more! πŸš€

Top comments (0)