Mastering Git: Essential Terminology Every Developer Should Know

Mastering Git: Essential Terminology Every Developer Should Know

What are Git and GitHub?

Git is a version control system that allows you to track file changes and collaborate with others. It is used to manage the history of your code and to merge changes from different branches.

GitHub is a web-based hosting service for Git repositories. GitHub is an online platform that allows you to store and share your code with others.

Terminology

Check your git version

The below command will display the current version of git installed on your system.

git --version        

Repository

A repository is a collection of files and directories that are stored together. It is a way to store and manage your code. Run the below command to see the current state of your repository:

git status        

Set your config settings

Let’s set up your email and username in this config file. I would recommend you create an account on Git Hub and then use the email and username that you have created.

git config --global user.email "[email protected]"
git config --global user.name "Your Name"        

How to create a repository?

Creating a repository is the process of creating a new folder on your system and initializing it as a Git repository. It’s just a regular folder to code your project, you are just asking git to track it.

git status
git init        

git status command will show you the current state of your repository. git init command will create a new folder on your system and initialize it as a git repository. This adds a hidden .git folder to your project.

Commit

A snapshot of your changes. Commits are like save points in your project’s history. Each commit has a unique ID.

git commit -m "commit message"
git status        

But before you commit your changes you need to add them so that they will go into the staging area and after this, you can commit and push your code to GitHub. Below is the complete flow diagram:


You can undo the git add command also if you have added some files by mistake, below are the commands for it

# Stage the file
git add example.txt

# Undo the staging
git reset example.txt

# Stage all files
git add .

# Undo the staging for all files
git reset
        

So when you need to add, commit and push your change use the below commands:

git add .   // use "." to add all the files
git commit -m "first comit"
git push        

Logs

This command will show you the history of your repository. It will show you all the commits that were made to the repository. You can use the --oneline flag to show only the commit message.

git log
git log --oneline        

Branches in Git

git branch

The git branch command is used to manage branches in your Git repository. It can list, create, delete, and rename branches. Some common commands are

git branch  // list all branches
git branch <branch-name>  //create a new branch
git branch -d <branch-name>  // delete a branch
git branch -m <old-branch-name> <new-branch-name> //rename a branch        

git switch

The git switch command is used to switch between branches or restore working tree files. It is a newer and more intuitive way to switch branches compared to the older git checkout command.

git switch <branch-name>  // switch to an existing branch
git switch -c <new-branch-name>  // create and switch to a new branch

------------------Same with git checkout--------------------------------
git checkout <branch-name>
git checkout -b <new-branch-name>        

Merging branches

So basically there are 2 types of merging:

Fast-forward merge

A fast-forward merge occurs when the branch being merged has no new commits compared to the branch you're merging into.

Example

  1. Suppose you have a main branch and a feature branch.
  2. If no new commits have been made to main since feature branched off, a fast-forward merge can occur.

# Switch to main branch
git checkout main

# Merge feature branch into main with fast-forward
git merge feature
        

Non-Fast-forward merge

A non-fast-forward (or true) merge occurs when there have been commits on both branches since they diverged. In this case, Git creates a new merge commit that combines the changes from both branches.

Example

  1. Suppose you have a main branch and a feature branch.
  2. If there have been commits on both branches since they diverged, a non-fast-forward merge is necessary.

# Switch to main branch
git checkout main

# Merge feature branch into main with a merge commit
git merge feature        

If the commands are the same, what is the difference between fast-forward and not fast-forward merge?

The difference is resolving the conflicts. In a fast-forward merge, there are no conflicts. But in a not fast-forward merge, there are conflicts, and there are no shortcuts to resolve them. You have to manually resolve the conflicts. Decide, what to keep and what to discard. VSCode has a built-in merge tool that can help you resolve the conflicts.

Rebase and reflog

Git Rebase

git rebase is a command that allows you to move or combine a sequence of commits to a new base commit.

Some people like to use rebase over the merge command because it allows you to keep the commit history cleaner and easier to understand. It also allows you to make changes to the code without affecting the original branch.

Basically, in the merge command, there can be multiple merge commits that are unnecessary and people don't like it but in the rebase command there will be no extra commits and this can be useful for keeping a cleaner, linear project history.

Example: Suppose you are working on the main branch and you have multiple commits and now need a new feature or a bug to resolve for which to create a separate branch called "buy-fixes". Now you start working on a new branch and have multiple commits over there also.

Now the problem is someone else is working on the main and he did a new commit but while merging your bug-fixes brand you also need the latest main branch code and when you merge the main with bug-fixes it creates a commit ID which just shows that merge and nothing else so this way if multiple merging happens then multiple merge commits IDs also generate which is where rebase comes into the picture. Generally, people think that an extra merge commit ID pollutes the commit history.

Ensure you are on the branch you want to rebase:

git checkout feature-branch
git rebase main        

Git reflog

Git reflog is a command that shows you the history of your commits. It allows you to see the changes that you have made to your repository over time. This can be useful for debugging and understanding the history of your project.

git reflog        

Recover lost commits or changes

If you accidentally deleted a branch or made changes that are no longer visible in the commit history, you can often recover them using the reflog. First, find the reference to the commit where the branch or changes existed, and then reset your branch to that reference.

git reflog <commit-hash>
git reset --hard <commit-hash>        


Below are some basic day-to-day use commands:

git stash   // Temporarily saves changes that aren’t ready to be committed. It allows you to switch branches without losing your work.
git stash apply   // give you back your changes  
git rm -rf *  // delete all the files        


要查看或添加评论,请登录

社区洞察

其他会员也浏览了