The latest from Z Ware.

10 Most Commonly Used GIT Commands

Mar 24, 2023 | Blog

Git is a distributed version control system designed to manage source code and track changes to files over time. It was created by Linus Torvalds in 2005 and has since become one of the most widely used version control systems in the world.

Git allows developers to create and manage branches of a project, making it easy to experiment with new features, work on different versions of a project simultaneously, and collaborate with other developers. It tracks every change made to a project, providing a complete history of revisions and the ability to easily revert to previous versions.

Git is also highly secure, with built-in features for authentication, encryption, and access control. It can be used with a wide range of operating systems and development environments, making it a flexible tool that can be adapted to suit a wide variety of needs.

Overall, Git is a powerful and versatile tool that provides efficient version control, robust security features, and powerful collaboration tools, making it an essential tool for developers working on complex or collaborative projects.

Using terminal commands over a graphical user interface (GUI) such as Visual Studio can have several advantages, including:

  • Speed: Command-line interfaces are generally faster than graphical user interfaces, especially for repetitive tasks or tasks that involve a large number of files.
  • Customization: Command-line interfaces are highly customizable and can be configured to meet the specific needs of a developer.
  • Automation: Many tasks can be automated using terminal commands, allowing developers to save time and increase productivity.
  • Scripting: Terminal commands can be used to write scripts that can automate complex tasks or workflows.
  • Portability: Terminal commands are generally platform-independent, meaning that they can be used on any operating system without requiring additional software or configuration.
  • Version Control: Git commands are more easily accessible and more powerful in the terminal than in a graphical interface, allowing developers to take full advantage of version control tools.

While graphical user interfaces can be useful for some tasks, using terminal commands can provide greater control, flexibility, and efficiency for many developers.

Let’s look at the most used Git commands:

  1. git clone: This command is used to create a local copy of a remote Git repository. It allows a user to download the entire repository from a remote location and create a local copy on their machine.

The syntax for the git clone command is as follows:

For example, to clone a repository located at https://github.com/user/repo.git, the following command would be used:

2. git fetch: This command is used to update the local repository with changes made to a remote repository. It downloads new commits, branches, and tags from the remote repository and stores them in the local repository, but does not merge them with the local copy of the code.

The syntax for the git fetch command is as follows:

For example, to fetch changes from a remote repository named “origin”, the following command would be used:

After running ‘git fetch’, the user can inspect the new changes made to the remote repository by running git log or git diff commands. However, the changes will not be merged with the local copy of the code until a git merge or git pull command is executed.

‘git fetch’ is useful when working on a project with multiple developers or on multiple machines. It allows a developer to synchronize their local copy of the code with the remote repository without automatically merging the changes. This gives the developer more control over how and when to integrate changes into their local copy of the code.

3. git checkout: This command is used to switch between different branches in a Git repository or to check out a specific commit or file.

The syntax for the ‘git checkout’ command is as follows:

To switch to a different branch, the name of the branch is specified:

For example, to switch to a branch named “feature-branch”, the following command would be used:

‘git checkout’ is a powerful command that allows developers to switch between different branches or commits and to access specific files in a repository. It is an essential tool for managing different versions of code and for debugging issues that may arise in specific versions or branches.

4. git init: This command is used to initialize a new Git repository in a new or existing project directory. When you run this command, Git creates a new .git subdirectory in the root of your project directory. This .git subdirectory contains all the necessary files and metadata required by Git to track changes in your project.

Here are the steps to initialize a Git repository using the “git init” command:

  1. Open a terminal or command prompt and navigate to the root directory of your project.
  2. Type “git init” and press enter.
  3. Git will create a new .git subdirectory in your project directory.
  4. You can now start adding files to your project and use Git to track changes and manage versions.

             The syntax for the “git init” command is:

Here, “directory” is an optional argument that specifies the name of the directory where you want to initialize a new Git repository. If you don’t specify a directory name, Git will use the current directory as the project directory and initialize a new repository in it.

For example, to initialize a new Git repository in a directory named “my-project”, you would run the following command:

If you want to initialize a new repository in the current directory, simply run the command without any arguments:

Note that running “git init” in an existing Git repository will not reinitialize the repository or overwrite any of its existing data. It will simply return an error message indicating that the repository already exists.

5. git commit: This command is used in Git version control system to save changes to a local repository. When you make changes to files in a Git repository, you can commit those changes to save them and make them a part of the repository’s history.

The “git commit” command takes the changes you have made in the staging area and creates a new commit with those changes. The commit message you provide will be used to describe the changes you have made.

Here is an example of how to use the “git commit” command:

This command will create a new commit with the changes made to the myfile.txt file and a commit message “Added a new line to myfile.txt”. The commit will be saved to the local Git repository.

6. git push: This command is used to upload local repository content to a remote repository. This command is used after you have made changes to your local repository and want to push those changes to a remote repository, such as GitHub, GitLab, or Bitbucket.

Here’s an example of how to use “git push” command:

If the branch is not yet tracked and only resides on the local machine, you need to run the command like this:

git push –set-upstream <remote branch> <branch name>, for example:

7. git status: This command is used in the Git version control system to display the status of the working directory and staging area. When you run git status in a Git repository, you will see a summary of any changes that have been made to your files since the last commit, as well as any files that are currently staged for commit. This can be helpful in keeping track of changes to your code and ensuring that you don’t accidentally commit files that shouldn’t be included in the repository.

The output of git status can vary depending on the state of the repository, but some common information it might display includes:

  • The branch you are currently on
  • The status of any changes to files in your working directory (e.g. “modified”, “deleted”, “untracked”)
  • The status of any changes to files that have been staged for commit (e.g. “changes to be committed”, “new file”)
  • Instructions for how to stage changes or commit them to the repository.

8. git pull: This command is used in the Git version control system to update the local repository with changes from a remote repository. It is essentially a combination of two separate commands: git fetch and git merge. When we run git pull, Git will first fetch any new commits or changes from the remote repository and update the local copy of the remote branch. Then, it will attempt to merge these changes into your current working branch.

If there are any conflicts between the changes made in the remote repository and the local changes, Git will prompt to resolve these conflicts before the merge can be completed. Once any conflicts are resolved, the changes will be merged into the local branch and the working directory will be updated to reflect the latest changes from the remote repository.

It is generally a good idea to run git pull before making any changes to the local repository to ensure that we are working with the latest version of the code. However, we should always review any changes that are pulled in from the remote repository to ensure that they do not conflict with the local changes.

9. git add: This Git command is used to add changes to the staging area.

When we make changes to files in the Git repository, those changes are initially untracked by Git. To track those changes, we can use the git add command to add them to the staging area. The staging area is a temporary area where we can review and organize our changes before committing them to the repository.

We can add specific files to the staging area by specifying their filenames, like this:

Or you can add all changes to the staging area using the `.` (dot) symbol, like this:

After adding our changes to the staging area, we can review them using the git status command. Once we are satisfied with the changes in the staging area, we can commit them to the repository using the git commit command.

10. git branch: git branch is a Git command that shows, creates, or deletes branches.

A branch in Git is a pointer to a specific commit in the Git repository’s history. When we create a new branch, we create a new pointer that points to the current commit. This allows us to work on a new feature or bug fix without affecting the main codebase.

Here are some of the most common git branch commands:

  • git branch: This command lists all the local branches in the Git repository. The current branch is highlighted with an asterisk (*).
  • git branch <branch-name>: This command creates a new branch with the given name at the current commit.
  • git branch -d <branch-name>: This command deletes the specified branch. However, Git will prevent deleting a branch that hasn’t been merged yet.
  • git branch -m <old-branch-name> <new-branch-name>: This command renames the specified branch.
  • git branch -a: This command lists all the branches, including both local and remote branches.

Conclusion

By learning and using these and other Git commands, developers can effectively collaborate with their team members, manage their code base, and keep track of changes over time.