π 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:
- 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 π.
- Local Git Repository π»: These repositories are hosted on your system.
- History of Code π: You can view your Git history by typing the command git log.
- Staging π: This is the area where you decide which changes to commit. Staging prepares the code for committing β .
- 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 π₯οΈ.
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 π.
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 π οΈ:
- Working directory π: Where new files are created.
- 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.
- 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 πΈ.
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:
Through the remote repository π:
Simply go to the branches section in your account and delete the branch π±οΈ.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/*
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>
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 πππ‘
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)