If you are hunting for some basic Git commands, then this is the right place for you! Before beginning with the commands, here are some insights about Git for you. If you haven't set it up yet, you can learn how to install and upgrade Git to get started on the right foot.

πŸš€ Git Workflow

Git is an open-source VCS project. It was established by Linus Torvalds in 2005. An immense amount of software projects depend on Git for version control. Git workflow is a very popularly used VCS (Version Control System) in the tech world. It aids you to keep a check on the changes carried out in a file, allowing for seamless collaboration across different environments, from local machines to a remote server.

Git can be easily used by everyone. You can avail of it on Windows, Linux, Solaris, and Mac. For a better understanding, let us tell you the important terms related to Git!

⭐ Important terminologies

Version Control System: Version Control Systems basically help various developers, team members, as well as designers to work on the same project at the same time.

Commonly known as VCS, such systems guarantee that every team member can access the latest code. The development process is a bit complex and hence more people are required to manage various versions of all the products.

Therefore, it becomes feasible for the entire team to have access to the code at the same time in order to carry out a parallel and seamless working environment.

Some popular types of VCS are:

  • SVN (Subversion)
  • Git (The most widely used)
  • TFS (Team Foundation Server)
  • Helix Core
  • Mercurial

Control System: A control system is basically a system that manages or directs other systems to reach a particular result. In short, a system that usually controls or regulates other systems is known as a control system.

Distributed Version Control System: There are several repositories in Git. It has a remote repository on the server, whereas the local repository is stored on every developer's computer system. This means that even if you are using a remote development environment, you still have a full backup of the project history locally.

Therefore, a distributed version control system simply implies that the code is available or is distributed on each member's system!

❓ Why is Git needed?

As you have read above that the version control system helps various developers to work together at the same time. Therefore, it becomes extremely easy for the developers who work on real-life projects to work in parallel. Without Git, managing overlapping changes would be a nightmare.

Moreover, in such projects, version control systems help to go to the older version of the code too. The developers avail a huge benefit from this feature, as they can "undo" mistakes without losing days of work.

Now, after understanding the basics, let us head towards some Git commands.

πŸ› οΈ Git commands

πŸ“‚ To create a local repository

In order to create a new repository in a new directory, with a specific project name, follow the git init command. This is the first step in starting any version-controlled project on your local machine or server.

git init [project name]

A new project adds up to your local Git repository, creating a hidden .git directory where all the tracking data is stored.

πŸ“ To stage a file

If you want to stage a file (prepare it for a commit), use this command. Staging allows you to pick exactly which changes you want to include in your next snapshot.

git add test.txt

For creating multiple files or staging multiple specific files, simply write the following command:

git add filename1 filename2 filename3

Next up, if you want to club up all the files of your project folder to the staging area at once, use this command:

git add .

πŸ‘₯ To clone - git clone

This command creates a copy of an existing repository. If you are moving a project to a new server, cloning is the fastest way to get your code there. To access the repository that is on a remote server, follow this command:

git clone username@host:/path/to/repository

If you seek to copy a local repository to another directory, type this command:

git clone /path/to/repository

πŸ’Ύ To commit - git commit

This command takes a snapshot of whatever changes you make in the staging area. After that, the changes are saved in the local Git directory history.

git commit –m "Initial commit message"

Remember that the committed changes do not store in the remote repository automatically; they are only saved locally until you push them.

πŸ” Status - git status

This command helps to display a list of all the modifications and changes made in the files. Moreover, you also get to know which files are present in the staging area and which are untracked. It is one of the most frequently used commands to keep track of your progress.

git status

πŸ“œ git Log

If you want to print all the commits that have taken place in the current branch, then follow this command:

git log

This command presents you with the SHA-1 hash of the commit, the name of the author, the date, and also the commit message.

🌿 git Branch

A branch is basically a pointer to the latest commit of a local Git repository. It allows you to work on new features without affecting the main codebase.

To create a new branch named "abc", simply follow:

git branch abc

Now, this command generates a new branch and simultaneously switches you to it. This is a shorthand way to create and enter a branch immediately:

git checkout -b <branchname>

Now, in order to switch between existing branches, use:

git checkout <branch-name>

Moving on, to make a list of all the branches available in your repository, use the following command:

git branch

For more advanced workflows, you can learn how to switch branches in Git efficiently.

βš™οΈ Configuration - git config

If you want to set some user-specific values, for instance, username, email, or file format, you use the config command. This is usually done once per system installation.

Here is a command to set up an email globally:

git config --global user.email abc@example.com

The global flag depicts that you can use the mentioned email for all the local repositories on your system. Additionally, for different repositories, you can set different emails by using the local flag:

git config --local user.email youremail@example.com

🌐 git remote

This command allows you to view all the remote repositories connected to your local one.

git remote -v

In order to establish a connection between a remote server and your local repository, follow this command:

git remote add origin <host-or-remoteURL>

Further, to delete the connection with a specific remote repository, use the below-stated command:

git remote rm <name-of-the-repository>

πŸ“€ Git push

If you want to push the code from your local repository to the remote repository's master branch, type the following command. This makes your local changes available to others on the remote server.

git push -u origin master

πŸ“₯ Git pull

This command helps you to pull all the latest changes into the local repository from a remote repository. It is essentially a combination of git fetch and git merge.

As the code of the remote repository keeps on getting updated by the developers of a team, git pull becomes important to stay synchronized.

git pull

βš–οΈ Git diff

This command helps you to see the differences between your files in different states. It can show conflicts against the base file:

git diff --base <file-name>

In between the branches, if there are any conflicts, you can view them before merging. Use the below-given command to compare the source and target branches:

git diff <source-branch> <target-branch>

Additionally, to view any current unstaged conflicts or changes, use the following command:

git diff

πŸ”„ Git reset

If you want to reset your working directory or the index to a specific state, follow this command. Be careful, as the --hard flag will discard all local changes.

git reset --hard HEAD

πŸ“¦ Git stash

If some changes cannot be committed yet but you need to switch branches, use this command to temporarily save (stash) your modifications:

git stash

πŸ—‘οΈ Git rm

If you want to delete some files from the working directory as well as the index, then use this command. This also stages the removal for your next commit.

git rm filename.txt

🏷️ Git tag

If you want to display all the tags in alphabetical order, type:

git tag

If you are seeking some elaborate information about a specific tag (like release notes), then follow this command:

git show v1.4

Furthermore, if you want to tag a previous commit for historical reference, mention the commit ID as stated below:

git tag -a v1.2 9fceb02

πŸ‘οΈ Git show

If you want any information about a Git object (like a commit, tag, or blob), simply type this command:

git show

πŸ” Git grep

By using this command, you can search your tracked files, working directory, and multiple items in a single file or staging area for specific words or phrases. It is much faster than standard search tools for large codebases.

For instance, if you want to search a particular email id or string, simply use this command:

git grep "search-string"

πŸ“‘ Git fetch

This command helps you to download files or objects from a remote repository to your own local repository without merging them. It's a safe way to see what others have been working on.

git fetch origin

🌐 Git instaweb

This command helps you to instantly browse the working repository in a web-based interface (git-web). It is quite useful for a quick visual overview.

git instaweb –httpd=webrick

πŸ–₯️ Gitk

If you are looking to view the local repository's graphical interface to see the commit history and branches visually, then follow this command :

gitk

πŸ” Git rebase

This command is used to move or combine a sequence of commits to a new base commit. It is often used to keep a clean, linear project history.

git rebase master

🧹 Git gc

You can remove the files that are not necessary to optimize your local repository and compress file revisions to save space.

git gc

πŸ“‹ Comparison Table: Common Git Commands

Command Primary Function Usage Scenario
git init Initialize Repo Starting a new project.
git add Stage Changes Preparing files for a commit.
git commit Record Changes Saving a snapshot of the code.
git push Upload Changes Sending code to a remote server.
git pull Download & Merge Updating local code from remote.

πŸ’‘ Pro Tips for Git Mastery

  • Use meaningful commit messages: Instead of "update", use "Fix login bug in auth controller".
  • Commit often, push once: Keep your local commits small and manageable, then push a finished set of changes.
  • Branches are free: Don't be afraid to create a branch for even small experiments.
  • Setup a Private Server: For sensitive projects, you can learn how to set up a private Git server on VPS to maintain full control over your data.

⚠️ Common Git Mistakes to Avoid

  • Committing sensitive data: Never commit passwords or API keys. Use a .gitignore file to exclude them.
  • Forgetting to pull before pushing: Always pull the latest changes to avoid unnecessary merge conflicts.
  • Hard resetting without a backup: git reset --hard is permanent; ensure you don't have unsaved work.

βœ… Conclusion

Bravo! Now you have attained knowledge about all the basic Git commands. If you are a developer, then retaining all these commands will prove to be extremely beneficial for you, whether you are working on a GitHub project or managing your own codebase. When you're ready to put these skills into practice, a Linux VPS gives you full root access to install Git and host your repositories exactly the way you want. We hope that this article helped you to gain knowledge and solve your ambiguities about the Git commands. Keep practicing and keep learning!

People also read: