Everything about Git (Version Control)

Niraj
6 min readOct 11, 2023

--

Git

Git is a version control system that helps you keep track of changes in your files and collaborate with others. It allows you to save different versions of your work, track who made what changes, and easily switch between different versions of your project. Think of it as a time machine for your code, enabling you to work on software projects more efficiently and collaboratively.

1. Git Init

git init is a Git command that initialises a new Git repository. When you run git init in a directory, it sets up the necessary data structures and configuration files that are required for version control using Git. Here's what it does:

  1. Initialisation: It creates a new .git directory in the root of your project. This directory is hidden by default and contains all the metadata and object database for your repository.
  2. Empty Repository: At this stage, the repository is empty. It doesn’t have any files or commit history. You can start adding files to your project and making commits.
  3. Configuration: git init also sets some initial configuration options for your repository. These configurations can be modified later using commands like git config.
  4. Branch: It creates a default branch called master (or main in more recent Git versions) where your initial commits will be stored.
git init

2. Git Config

git config is a Git command used to set, get, or remove configuration variables for your Git environment. Git uses these configuration settings to determine how it operates and to store information about your identity, repository settings, and more.

Global configurations are specific to a single user and all their repositories on a specific system. They are stored in the ~/.gitconfig file. You can set these using the --global flag with git config.

git config --global user.name "Your Name"

Local configurations are specific to a single Git repository. They are stored in the repository’s .git/config file. You can set these configurations without any flags, and they will only apply to the current repository.

git config user.email "youremail@example.com"

To check your Git name & email configuration, you can use:

git config user.email
git config user.name

To set your email for the current repository:

git config user.email "your.email@example.com"

3. Git Clone

git clone is a command used to create a local copy of an existing source code repository from a remote location, such as GitHub. This command allows you to download and store an identical copy of the latest version of a project on your computer, making it accessible for you to work with and modify as needed.

git clone <https://name-of-the-repository-link>

To download a project from GitHub, simply click on the green “Clone or download” button, copy the URL provided, and then paste it after the git clone command, as demonstrated above.

This action will create a copy of the project in your local workspace, allowing you to begin working on it.

4. Git branch

Branches are highly important in the git world. By using branches, several developers are able to work in parallel on the same project simultaneously. We can use the git branch command for creating, listing and deleting branches.

Creating a new branch:

git branch <branch-name>

This command will create a branch locally. To push the new branch into the remote repository, you need to use the following command:

git push -u <remote> <branch-name>

Viewing branches:

git branch or git branch --list

Deleting a branch:

git branch -d <branch-name>

5. Git checkout

Switching between branches is a fundamental aspect of working with Git. To work within a specific branch, you use the git checkout command. It allows you to switch from one branch to another. Additionally, you can employ git checkout to check out files and specific commits.

git checkout <name-of-your-branch>

There is also a shortcut command that allows you to create and switch to a branch at the same time:

git checkout -b <name-of-your-branch>

This command creates a new branch in your repository and immediately switches to that new branch after it has been created

6. Git status

The git status command provides essential information about the current branch in Git. Git status tells us if the current branch is up to date, if there are changes to commit, push, or pull, and the status of files (staged, unstaged, or untracked), including file modifications and deletions.

git status

7. Git add

When we create, modify, or delete a file, these changes will be made in our local workspace and won’t be included in the next commit unless we stage and commit these changes or modify the configuration settings.

We have to use the git add command to add the changes of a files into our next commit.

To add single file:

git add <file>

To add everything at once:

git add .
git add -A

8. Git commit

Git commit is one of the most frequently used Git commands. It serves as a checkpoint in the development process, allowing you to save your changes, typically after completing a specific task or issue. This checkpoint can be revisited later if needed.

Git commit saves your changes only locally:

git commit -m "commit message"

9. Git push

After committing your changes, the next step is to use git push to upload your commits to the remote repository.

git push <remote> <branch-name>

If your branch is newly created, then you also need to upload the branch with the following command:

git push -u origin <branch_name>

Important: Git pushes only those changes that are committed.

10. Git pull

The git pull command is used to update your local repository with changes from the remote repository. It combines git fetch to retrieve updates and git merge to apply the latest changes to your local branch

This operation may cause conflicts that you need to solve manually.

git pull <remote>

11. Git revert

At times, there’s a need to undo changes that have been made. Whether locally or remotely, it’s crucial to use these commands carefully to prevent unintended deletions.

To get a hash code:

git log -- oneline

Then we just need to specify the hash code next to our commit that we would like to undo:

git revert 3321844

The Git revert command will undo the given commit, but will create a new commit without deleting the older one.

12. Git merge

When you’ve finished development in your branch, ensuring that everything is functioning correctly, the final step is merging the branch with the parent branch, typically ‘dev’ or ‘master.’ This merging process is accomplished using the ‘git merge’ command.

‘Git merge’ essentially integrates your feature branch, along with all its commits, back into the ‘dev’ or ‘master’ branch. It’s important to note that you should be on the specific branch you wish to merge with your feature branch before executing the merge.

First you should check all the branch:

git checkout <branch-name>

Before merging, you should update your local dev branch:

git fetch

Finally, you can merge your feature branch into dev:

git merge <branch-name>

--

--