Some of The Most Commonly Used Basic Git Commands

Some of The Most Commonly Used Basic Git Commands

Basic Git Commands: Git is a distributed version control system and open-source software that you can use to manage your projects. If you’ve ever needed to track code changes, commit your changes, or revert them later, Git is for you. It allows developers to manage many versions of a source code with ease. With Git version control software you can find out who committed what, when, and why.

Now, Git become one of the most used tools for any developer. And if you want to use Git, then you need to know some of the most commonly used basic Git commands. There are many basic git commands, but a few are mostly used. In this article, you will learn about the most commonly used basic Git commands that will take you to the next level of development. So let’s get started and learn about the most commonly used basic git commands that we are going to discuss in this article.

To download Git visit https://git-scm.com/download/win

Most Used Basic Git Commands

Git is a widely used version control system that allows developers to manage and track changes to their code. Whether you’re a beginner or an experienced developer, there are some basic Git commands you’ll need to know to get started with Git. In this article, we will discuss the most commonly used basic Git commands that every developer should know.

git config

The ‘git config’ command is used to configure Git on your computer. This is one of the most basic git commands which allows you to set configuration variables that control Git’s behavior, such as your name and email address, your preferred editor, and your default branch.

Here are some examples of how you can use the ‘git config’ command:

  • To set your name and email address:
git config --global user.name "John Doe" 
git config --global user.email johndoe@example.com
  • To set your default editor:
git config --global core.editor nano
  • And to Set your default branch:
git config --global init.defaultBranch main

You can also use ‘git config’ to view your current Git configuration, by running ‘git config –list’ or ‘git config –global –list’ to list all of your global configurations.

Learn More: How to delete a branch in git

git init

The ‘git init’ command is used to initialize a new Git repository. It is also basic git commands which creates a new .git subdirectory in the current working directory, which contains all of the necessary files and directories for Git to track changes to your project’s source code.

Open a terminal or command prompt and navigate to the directory where you want to create your new repository:

  • Run the ‘git init’ command:
git init
  • This will create a new ‘.git’ subdirectory in the current working directory:
  • Add your project’s source code to the repository by creating files and directories as needed:
  • Use the ‘git add’ command to stage changes to your project’s source code:
git add.
  • This will stage all changes in the current directory and its subdirectories:
  • Use the ‘git commit’ command to save your changes to the repository:
git commit -m "Initial commit"
  • This will create a new commit in the repository’s history, containing all of the changes that you have staged:

After you have initialized your Git repository, you can use other Git commands to manage and track changes to your project’s source code. A ‘git init’ command is an essential tool for anyone who wants to use Git to manage their source code.

git clone

The git clone command is used to create a copy of an existing Git repository on your local machine. It is used when you want to start working with an existing Git repository, either on your own machine or on a remote server like GitHub.

Here’s how you can use the git clone command to clone an existing Git repository:

  • Open a terminal or command prompt and navigate to the directory where you want to clone the repository:
  • Find the repository’s URL. This will usually be a URL that starts with git:// or https://. If you are cloning a repository from a remote server like GitHub, you can find the URL by clicking the “Clone or download” button on the repository’s page:
  • Run the git clone command with the repository’s URL as an argument:
git clone https://github.com/username/repo.git
  • Replace the username and repo with the actual username and repository name:
  • Wait for the repository to be cloned. This may take a few minutes depending on the size of the repository and your internet connection speed:

After the repository has been cloned, you can start working with its source code using Git commands such as git add, git commit, and git push.

git branch

The git branch command is used to manage Git branches in your local repository. A branch is essentially a separate line of development that allows you to work on different features or versions of your project simultaneously without affecting the main codebase.

Here are some examples of how you can use the git branch command:

  • To list all branches in the repository.
git branch
  • To create a new branch:
git branch new-branch
  • This will create a new branch called the new branch based on your current branch.
  • To switch to a different branch:
git checkout other-branch
  • This will switch to the other-branch branch.
  • To create and switch to a new branch in one command:
git checkout -b new-branch
  • This will create a new branch called new-branch and switch to it.
  • To delete a branch:
git branch -d branch-to-delete
  • This will delete the branch-to-delete branch.

git checkout

The git checkout command is used to switch between different branches or commits in your Git repository. It allows you to navigate between different versions of your code and work on different branches simultaneously.

Here are some examples of how you can use the git checkout command:

  • To switch to a different branch:
git checkout other-branch
  • This will switch to the other-branch branch.
  • To create and switch to a new branch in one command:
git checkout -b new-branch
  • This will create a new branch called new-branch and switch to it.
  • To switch to a specific commit:
git checkout 123abc
  • This will switch to the commit with the SHA-1 hash 123abc.
  • To discard changes in your working directory:
git checkout -- file.txt
  • This will discard any changes to the file.txt file in your working directory and revert it to the state in the last commit.

git add

The ‘git add’ command is used to stage changes to your Git repository’s index. It prepares changes to your files to be committed and saved in the repository’s history.

Here are some examples of how you can use the git add command:

  • To stage changes to a single file:
git add file.txt
  • This will stage changes to the file.txt file.
  • To stage changes to all files in the current directory and its subdirectories:
git add .
  • This will stage changes to all files in the current directory and its subdirectories.
  • To stage changes interactively:
git add -i
  • This will start an interactive session where you can choose which changes to the stage and which to ignore.

Once changes have been staged using the git add command, they can be committed to the repository using the git commit command. The git add command is an essential tool for managing changes to your Git repository and keeping track of your project’s history.

git push

The git push command is used to upload local repository content to a remote repository. It is used to share changes you’ve made to your local Git repository with a remote repository, such as a repository hosted on Bitbucket, GitHub, or GitLab.

Here are some examples of how you can use the git push command:

  • To push changes to the default branch of a remote repository:
git push
  • This will push the changes to the default branch of the remote repository.
  • To push changes to a specific branch of a remote repository:
git push origin branch-name
  • This will push the changes to the branch-name branch of the remote repository.
  • To push changes to a new branch:
git push -u origin new-branch
  • This will push the changes to the new-branch branch of the remote repository and set it as the default branch for future pushes.

A git push command is an essential tool for collaborating with other developers on a project. It allows you to share your changes with others and keep your local and remote repositories in sync.

To Delete Repo on Git Read: How To Delete A Repository In GitHub

git pull

The git pull command is used to update the local repository with the latest changes from a remote repository. It combines the git fetch command, which retrieves the latest changes from the remote repository, with the git merge command, which merges the changes into the current branch.

Here are some examples of how you can use the git pull command:

  • To update the current branch with the latest changes from the default branch of a remote repository:
git pull
  • This will fetch the latest changes from the default branch of the remote repository and merge them into the current branch.
  • To update the current branch with the latest changes from a specific branch of a remote repository:
git pull origin branch-name
  • This will fetch the latest changes from the branch-name branch of the remote repository and merge them into the current branch.

A git pull command is an essential tool for keeping your local repository up-to-date with the latest changes from a remote repository. It allows you to collaborate with other developers on a project and ensure that everyone is working with the latest version of the code.

git merge

The git merge command is used to combine changes from one branch into another. It allows you to integrate changes made in one branch into another branch, such as the master branch. However, it’s important to be cautious when merging branches, as conflicts can arise and it’s important to resolve them before committing the changes.

Here are some examples of how you can use the git merge command:

  • To merge changes from a branch into the current branch:
git merge branch-name
  • This will merge the changes from the branch-name branch into the current branch.
  • To merge changes from a branch and preserve its commit history:
git merge --no-ff branch-name
  1. This will merge the changes from the branch-name branch into the current branch while preserving its commit history.
  2. To abort a merge that has conflicts:
git merge --abort
  • This will abort the merge process and return the branch to its previous state.

A git merge command is an essential tool for integrating changes from one branch into another. It allows you to collaborate with other developers on a project and ensure that everyone is working with the latest version of the code. However, it’s important to be cautious when merging branches, as conflicts can arise and it’s important to resolve them before committing the changes.

git status

The git status command is used to display the current state of the Git repository. It shows which files have been modified, which files are staged for the next commit, and which files are not being tracked by Git. It helps you keep track of which files have been modified and which files are ready to be committed to the repository.

Here are some examples of how you can use the git status command:

  • To show the current state of the repository:
git status
  • This will display the current state of the repository, including which files have been modified and which files are staged for the next commit.
  • To show the current state of the repository in a more detailed format:
git status -s
  • This will display the current state of the repository in a more detailed format, including which files have been modified, which files are staged for the next commit and the status of each file.
  • To show the current state of the repository for a specific path:
git status path/to/file

This will display the current state of the repository for the ‘path/to/file’, including whether it has been modified, staged, or not tracked by Git.

git clean

The git clean command is used to remove untracked files from the working directory of a Git repository. Untracked files are files that are not being tracked by Git, which means they are not part of the repository’s commit history.  However, it’s important to be cautious when using this command, as it can permanently delete files from your system. Make sure to review the list of files that will be removed before running the git clean command.

Here are some examples of how you can use the git clean command:

  • To list the untracked files that will be removed:
git clean -n
  • This will list the untracked files that will be removed from the working directory.
  • To remove the untracked files:
git clean -f
  • This will remove the untracked files from the working directory.
  • To remove untracked files and directories:
git clean -fd
  • This will remove the untracked files and directories from the working directory.

git commit

The git commit command is used to save changes to the local repository. When you make changes to files in a Git repository, those changes are not automatically saved to the repository. Instead, you need to use the git commit command to create a new commit that records the changes.  It’s important to write clear and descriptive commit messages that accurately describe the changes you’ve made to the repository. This makes it easier for you and other developers to understand the history of the project and track changes over time.

Here are some examples of how you can use the git commit command:

  • To commit changes to all tracked files:
git commit -a -m "Commit message"
  • This will commit changes to all tracked files and include a message describing the changes.
  • To commit changes to specific files:
git commit file1.txt file2.txt -m "Commit message"
  • This will commit changes to file1.txt and file2.txt and include a message describing the changes.
  • To amend the previous commit:
git commit --amend -m "New commit message"
  • This will amend the previous commit with a new commit message.

git commit –amend

The git commit –amend command is used to make changes to the most recent commit in a Git repository. This can be useful if you forgot to include a file in the previous commit, need to update the commit message, or want to make other changes to the previous commit.

Here are some examples of how you can use the git commit –amend command:

  • To add changes to the previous commit:
git add file1.txt
git commit --amend
  • This will add the changes to file1.txt to the most recent commit in the repository.
  • To update the commit message:
git commit --amend -m "New commit message"
  • This will update the commit message of the most recent commit in the repository.
  • To add changes and update the commit message:
git add file1.txt
git commit --amend -m "New commit message"

This will add the changes to file1.txt to the most recent commit in the repository and update the commit message.

git fetch

The git fetch command is used to retrieve the latest changes from a remote Git repository without automatically merging those changes into your local repository. When you run git fetch, Git will retrieve any new branches, commits, and files from the remote repository and store them in a separate branch called a “remote branch”.

Here are some examples of how you can use the git fetch command:

  • To fetch changes from the default remote repository:
git fetch
  • This will retrieve the latest changes from the default remote repository (usually called origin) and store them in remote branches.
  • To fetch changes from a specific remote repository:
git fetch <remote>
  • This will retrieve the latest changes from the specified remote repository and store them in remote branches.
  • To view the list of remote branches:
git branch -r

This will show a list of remote branches that you can merge or checkout.

The git fetch command is useful for keeping your local repository up-to-date with changes in the remote repository. However, it’s important to note that git fetch does not automatically merge those changes into your local repository. To merge the changes into your local repository, you’ll need to use the git merge or git pull command.

git log

The git log command is used to display the commit history of a Git repository. When you run the git log, Git will display a list of commits in reverse chronological order, with the most recent commit appearing first. It can be used to see who made changes to the repository, when those changes were made, and what those changes were. By default, the git log displays a lot of information about each commit, including the commit message, author, and timestamp. However, you can use various options and filters to customize the output of the git log to suit your needs.

Here are some examples of how you can use the git log command:

  • To display the commit history:
git log
  • This will display the commit history of the current branch, with each commit listed in reverse chronological order.
  • To limit the number of commits displayed:
git log -n <number>
  • This will display the specified number of commits, with each commit listed in reverse chronological order.
  • To display a specific branch’s commit history:
git log <branch>
  • This will display the commit history of the specified branch, with each commit listed in reverse chronological order.
  • To display the commit history for a specific file:
git log <file>

This will display the commit history for the specified file, with each commit listed in reverse chronological order.

git rebase

The ‘git rebase’ command is used to modify the commit history of a Git repository by applying a sequence of commits on top of another branch. This allows you to change the order, content, or structure of commits in the repository’s history.

When you run ‘git rebase’, Git will first find the common ancestor of the current branch and the target branch. It will then apply the changes introduced by each commit in the current branch, one by one, on top of the target branch. This creates a new sequence of commits that incorporates the changes from both branches.

Here are some examples of how you can use the ‘git rebase’ command:

  • To rebase a branch on top of another branch:
git checkout <branch>
git rebase <target-branch>
  • This will apply the changes in ‘<branch>’ on top of ‘<target-branch>’. You may need to resolve conflicts during the rebase process.
  • To rebase a branch interactively:
git checkout <branch>
git rebase -i <commit>
  • This will open an interactive rebase editor, allowing you to modify the order, content, or structure of commits in the current branch. You can squash or edit commits, reorder them, or drop them entirely.
  • To abort a rebase:
git rebase --abort

This will abort the current rebase operation and restore the repository to its previous state.

git rebase -i

The ‘git rebase -i’ command is used to perform an interactive rebase in Git. An interactive rebase allows you to modify the commit history of a branch by interactively editing a list of commits.

When you run ‘git rebase -i’, Git will open an interactive editor that displays a list of the commits in the current branch. Each commit is represented by a single line of text that includes the commit hash, author, and commit message.

You can then edit this list to modify the order, content, or structure of commits in the current branch. For example, you can squash multiple commits into a single commit, edit commit messages, or remove commits entirely.

Here are the basic steps to perform an interactive rebase using ‘git rebase -i’.

  • Check out the branch you want to rebase:
git checkout <branch>
  • Start the interactive rebase:
git rebase -i <commit>
  • The ‘<commit>’ argument specifies the commit at which you want to start the rebase. This is usually the parent of the first commit you want to modify.
  • Edit the commit list:
  • The interactive editor will display the list of commits in the current branch. You can modify this list by:
      • Reordering commits by rearranging the lines.
      • Squashing commits by replacing ‘pick’ with ‘squash’ or ‘fixup’ in the commit message.
      • Editing commit messages by replacing the commit message text.
  • Once you’ve made your edits, save and close the editor.
  • Resolve conflicts (if necessary):
  • Git may stop the rebase process if there are conflicts between the modified commits and the target branch. You will need to resolve these conflicts before you can continue the rebase.
  • Complete the rebase:
  • Once you’ve resolved any conflicts, you can continue the rebase by running:
git rebase --continue

Git will apply the modified commits in the order you specified and create a new branch with the modified commit history.

git reflog

The ‘git reflog’ command is used to view the Git reference log, which records the history of changes to the Git repository’s references (such as branch and tag names) over time.

Each time you create, delete, or update a reference in Git, the reference log records the change along with a commit message and the commit hash of the commit that was affected by the change. This allows you to see the complete history of changes to your repository’s references, even if you’ve deleted or overwritten them.

Here are some examples of how you can use the ‘git reflog’ command:

  • To view the complete reference log:
git reflog
  • This will display a list of all the changes to the repository’s references, along with the commit message and commit hash for each change.
  • To view the reference log for a specific branch:
git reflog <branch>
  • This will display a list of all the changes to the specified branch’s references, along with the commit message and commit hash for each change.
  • To restore a deleted branch or commit:
git reflog
git checkout -b <new-branch> <commit-hash>
  • This will create a new branch called ‘<new-branch>’ at the specified commit hash, allowing you to restore a deleted branch or commit.

However, it’s important to note that the reference log only records changes to Git’s internal references, not changes to the contents of your files or directories. If you’ve accidentally deleted or modified a file, you may need to use Git’s other tools (such as ‘git checkout’ or ‘git reset’) to recover the lost data.

git remote

The ‘git remote’ command is used to manage remote repositories in a Git project. A remote repository is a version of your Git repository that is hosted on a different server, allowing multiple developers to collaborate on a single codebase.

Here are some examples of how you can use the ‘git remote’ command:

  • To list the remote repositories currently configured for your project:
git remote
  • This will display a list of the names of the remote repositories associated with your Git project.
  • To add a new remote repository:
git remote add <name> <url>
  • This will add a new remote repository to your Git project, with the specified name and URL. The URL should be the location of the remote repository, such as a Git hosting service like GitHub or GitLab.
  • To remove a remote repository:
git remote remove <name>
  • This will remove the remote repository with the specified name from your Git project.
  • To rename a remote repository:
git remote rename <old-name> <new-name>
  • This will rename the remote repository with the specified old name to the new name.
  • To show information about a specific remote repository:
git remote show <name>

This will display information about the specified remote repository, such as the URL, the default branch, and any configured fetch or push URLs.

git reset

The ‘git reset’ command is used to reset the current branch to a specific commit or to unstage changes from the staging area (also known as the index). But it’s important to use it carefully. Resetting a branch can permanently discard changes, so make sure to create a backup or branch before running the ‘git reset’ command.

Here are some examples of how you can use the ‘git reset’ command:

  • To unstaged changes from the staging area:
git reset
  • This will unstage all changes that have been added to the staging area, effectively undoing the previous ‘git add’ command.
  • To reset the branch to a specific commit:
git reset <commit>
  • This will reset the current branch to the specified commit, discarding any changes made since that commit. By default, the ‘git reset’ command will preserve changes in the working directory, but you can use the ‘–hard’ option to discard those changes as well.
  • To reset the branch to a specific commit and discard changes:
git reset --hard <commit>
  • This will reset the current branch to the specified commit and discard any changes made since that commit, both in the staging area and the working directory.
  • To reset the branch and move changes to the staging area:
git reset <commit> --soft
  • This will reset the current branch to the specified commit, but keep the changes made since that commit in the staging area.

git revert

The ‘git revert’ command is used to undo a specific commit by creating a new commit that contains the inverse changes.

Here are some examples of how you can use the ‘git revert’ command:

  • To revert the most recent commit.
git revert HEAD
  • This will create a new commit that undoes the changes made in the most recent commit.
  • To revert a specific commit.
git revert <commit>
  • This will create a new commit that undoes the changes made in the specified commit.
  • To revert multiple commits.
git revert <commit1> <commit2>
  • This will create a new commit for each specified commit, undoing the changes made in each one.

When you run the ‘git revert’ command, Git will open a text editor where you can enter a commit message for the new revert commit. Once you’ve entered the commit message and saved the file, Git will create a new commit that undoes the changes made in the original commit.

Unlike the ‘git reset’ command, the ‘git revert’ command does not discard any changes. Instead, it creates a new commit that reverses the changes made in a previous commit, which is a safer approach if you want to preserve the commit history of your Git repository.

Conclusion of basic git commands

Finally, Git is a powerful version control system that allows developers to manage their code repositories efficiently. Some basic Git commands that every developer should know to include:

  1. git init – initializes a new Git repository
  2. git clone – copies an existing Git repository to a local machine
  3. git add – adds files to the staging area
  4. git commit – commits changes to the repository
  5. git push – pushes committed changes to a remote repository
  6. git pull – pulls changes from a remote repository to a local repository
  7. git status – shows the status of files in the repository
  8. git log – displays a log of commit history
  9. git branch – shows a list of branches in the repository
  10. git checkout – switches to a different branch or commit

These basic git commands are just the basics, but they are essential for working with Git. Developers can also find more advanced commands to further enhance their Git workflow.

Learn More: How To Delete A Repository In GitHub,

1 thought on “Some of The Most Commonly Used Basic Git Commands”

  1. Pingback: Git Merge Tutorial - How Git Merge Works? - Techcoders

Comments are closed.

Scroll to Top