GIT Tutorial
Handling software development has undergone a revolutionary shift thanks to Git. It is an open-source distributed version control system with remarkable features. Despite an easy-to-use interface, Git can handle large projects with ease. No wonder, it has become the industry gold standard.
However, diving into a new tool like Git can be intimidating, especially for beginners. So, here's a straightforward Git tutorial. It aims to help professionals get Git up and running in no time! It is time to begin!
Setting Up Git
Before starting to use Git, one needs to download and install it on their systems. Here are the steps to do that:
- The first step is to visit the Git website at https://git-scm.com/downloads.
- After this, one must download the Git installer for the operating system. When using Windows, selecting the correct architecture (32-bit or 64-bit) is crucial.
- Once downloaded, the installer will run. The user should follow the installation wizard.
- During the installation process, the user must choose options like the destination folder. The default options should be enough for most users.
- After the installation is complete, the user needs to open the command prompt or terminal on the system.
- In the next step, the following command must be entered to verify Git installation:
- git --version
- After proper installation, this should display the version number of Git.
Git is now part of the system. However, some configuration options must be set up before one starts using it. Here's a step-by-step guide:
- The user must open the command prompt or terminal on the system.
- They need to enter the following command to configure the name and email address:
- git config --global user.name "User's Name"
- git config --global user.email "useremail@example.com"
- Alternatively, people can also set other configuration options. These include the preferred text editor and merge tool. Here are some examples:
- git config --global core.editor "nano"
- git config --global merge.tool "vimdiff"
- One needs to replace 'nano' and "vimdiff" with the preferred text editor and merge tool, respectively.
The user has now downloaded and installed Git on their system and set up the basic configuration. The next section highlights how to create a new Git repository.
Creating a New Git Repository and Making the First Commit
To create a Git repository, one must follow the steps mentioned below:
- One must open the terminal or command prompt on the system.
- Next, they need to navigate to the preferred directory where the repository will be created. They can use the cd command to change directories. For example, to create a repository in the Documents folder, typing cd Documents will do.
- Once inside the directory, the user needs to type the following command:
git init
4. This command initializes a new Git repository in the current one.
Now that the Git repository is initialized, the user can start adding some files to it and make their first commit.
- Firstly, they must create a new file in the repository directory. To do this, they must right-click and select 'New File.' Alternatively, they can use a text editor and save the file in the repository directory.
- Once the file is created, they must open the terminal or command prompt and navigate to the repository directory using the 'cd' command.
- To add the file to the repository, they need to use the 'git add' command followed by the file name. For example, if the file is called "thefile.txt", they can add it to the repository using the command git add thefile.txt
- After adding the file, they must commit the changes. This creates a snapshot of the repository with the new file added to it. To make a commit, the user must use the "git commit" command followed by a commit message in quotes. The commit message should be a brief description of the changes made in this commit. For example:
- git commit -m "Added myfile.txt to the repository"
Collaborating with Git
Git allows different developers to work on the same codebase together. Here's how one can collaborate with others using Git:
Cloning a Repository
To clone an existing repository, the user must use the git clone command followed by the URL of the remote repository. For example:
git clone https://github.com/username/repo.git
Push and Pull Changes
After making changes to the repository, the user needs to use the git push command to upload the changes to the remote repository. Other developers can then use the git pull command to download changes made by said user and merge them with their local repository.
Branching and Merging
Branching and merging are important concepts in Git. These allow users to work on different versions of their codes without interfering with each other's work.
Branching
Creating a new branch in Git means creating a new version of their codebase independent of the main branch. They can then work on this branch without affecting the main branch or other branches.
To create a new branch, they must use the "git branch" command followed by the branch name. For example, if they want to create a new branch called "feature", they can use the following command:
git branch feature
Merging
Once changes to the branch are complete and the user wants to incorporate those changes back into the main branch, they can use the merge command. The merge command combines the changes in one branch with the changes in another branch.
To merge changes from one branch into another, the user first needs to switch to the branch that they want to merge into (usually the main branch). For this, they can use the "git checkout" command followed by the branch name.
Then, they can use the "git merge" command followed by the name of the branch that they want to merge from. For example, if they want to merge changes from the "feature" branch into the main branch, they can use the following commands:
git checkout main
git merge feature
Resolving Conflicts
Conflicts occur in Git when two or more branches have made changes to the same file or lines of code. Git cannot merge these changes on its own. It needs manual intervention to resolve the conflicts.
When users attempt to merge two branches with conflicting changes, Git will give them a message indicating that there are conflicts. They can use the 'git status' command to see which files have conflicts.
They then need to open the file(s) with conflicts and resolve them manually. This may involve deleting some lines of code, editing others, or keeping both sets of changes.
After resolving the conflicts, they need to stage the changes using the "git add" command and then commit the changes using the "git commit" command.
Basic GIT Commands
Users must understand some basic Git commands to help manage a repository. Here are some important Git commands with examples:
- Git diff tells the difference between two versions of a file. Users can use this command to see what changes they have made to a file since the last commit.
- The Git log displays the commit history of the repository. The user can see the author, date, and commit message for each commit.
- The Git branch allows users to create a new branch in the repository. A branch is a separate version of the repository. Users can make changes here without affecting the main branch.
- Git reset is used to undo changes made in the repository. For example, to undo the last commit, users can use the following command:
- git reset --hard HEAD^
- This will reset the repository to the previous commit.
- Git stash temporarily saves changes that are not committed yet. For example, to save changes in the current branch, users can use the command:
- git stash.
- On the other hand, to apply the stash later, they can use the command:
- git stash apply
Understanding these commands makes it easier to use Git.
Conclusion
While Git is primarily used in software development, it can also be useful in other contexts. For example, in writing or project management, Git helps immensely. It is suitable for any situation where version control is important.
Git is not just a tool for individual developers, but also for collaborating teams. On one hand, it allows developers to work alone on different features or issues. And, on the other, they can then merge their changes back into the main codebase without code conflicts.
It provides a means for reviewing changes before merging them into the main codebase. Code reviews can help catch errors or issues before they cause problems in the future.
Git is a powerful tool but it can be overwhelming at first. Hence, one must start with the basics and gradually build up.
Like Git, many aspects of software development can help professionals give their CVs an added edge. Cogent University is committed to empowering professionals with the right skills and knowledge and kickstart their career in tech. Visit Cogent University to know more.