Top 10 Most Used Git Commands
What Is Git ?
Git is a version control system. Version control system is software that helps track and manage changes made to a set of files over time. Git is the most popular of them (alternative: subversion, CVS, mercurial). Using Git has almost become a standard nowadays and is of great importance to developers. There are hundreds of Git commands, but just a few are used regularly.
What Is GitHub ?
GitHub is a web service what it does is fetch git repositories. GitHub makes it possible to keep the code in the cloud and share it with other developers. Besides, it is important for open source projects. GitHub has become the home of open source projects on the internet today.
1. git init
In order to perform git operations on a repo in our locale, we need to turn that repo into a git repo. The init command means "initialise"; this is useful as it is the command that manages the entire initial setup of a repository. It create a new git repository and
$ git init
2. git status
We use the command below view the states of my project files. It shows status and changes in git repository.
$ git status
3. git add
When we create, modify or delete a file, these changes will happen in our local and won't be included in the next commit. It allows us to classify commits and import changes into the staging area.
$ git add <file-name>
4. git commit
It is a checkpoint in time. It is a snapshot of changes in our repository. With git commit, we get the changes in the staging area to the repo. When git commit, we write a message with the -m parameter. (Like this
git commit -m "message") This message should summarize the changes in that commit.
$ git commit -m "<message>"
5. git clone
It clones a repository to its local environment. In other words, git clone basically makes an identical copy of the latest version of a project in a repository and saves it to your computer.The repo does not need to be on github to use this command. It can be any platform. example: gitlab, bitbucket etc.
$ git clone <repo-url>
6. git remote
It allows you to show which remotes are currently connected, but also to add new connections or remove existing ones. Usually origin is used for name.
$ git remote -v
List all remote connections.
$ git remote add <name> <url>
Create a new remote connection.
$ git remote rm <name>
Delete a connection to a remote repository.
7. git push
Add changes in the local git repository to the remote repository.
$ git push <remote> <branch>
It pushes local repository to the remote repository.
$ git push -u origin main
This tells git that I want to push the master branch to origin main in my repo locally on my computer and I want it to remember that. i.e. it tells it to remember that the master branch on my computer is connected to the master branch on origin. In this way, if we do a push on this branch later, it will be enough to just write
8. git branch
List all local branches.
$ git branch -a
List all remote branches as well.
$ git branch <new-branch>
Create a new branch.
$ git branch -d <branch>
Delete the specified branch.
9. git switch
It allows us to switch branches, switch between branches.
$ git switch <branch-name>
10. git pull
It takes the changes from which branch we are pulling from and merges them with the branch we are working on locally. There is a possibility of conflict, so be careful.
$ git pull <remote> <branch>
So What Tools Do We Use Git With ?
GitLens is one of the best VS Code extensions for Git.
It can help you to know when, why, and whom a line or code block was changed, you can also jump back through history to gain insights into the code's evolution. You can explore the history and evolution of the codebase easily using GitLens.
2. Git History
Git History is a VS Code extension that lets you quickly investigate the history of a line or file. You’ll have access to commit information, view diffs, etc.
Git History gives us a wide range of features. Using Git History we can access the git log with the graph and details. We can view and search the history and we can also compare branches, commits, and files across commits along with many miscellaneous features.
3. Git Graph
Git Graph is another of the most important VS Code extensions that makes git easier to use.
Git stores history as a graph of snapshots of the entire repository. These snapshots, called commits in Git, can have multiple parents, creating a history that looks like a graph instead of a straight line.
Using Git Graph you can easily perform Git actions from the graph. It provides a Git Graph of your repository and also it is configurable to look the way you want.
So, these are our 10 most-used git commands and VS Code extensions we use that make it easy to use git.