Useful git commands that you should know

Git is one of the most popular version control tools among software developers, and they used git commands regularly in their projects. Most of the software companies used Git to manage the project versions. So, if you are planning to join a software development team where more than one developer works on a project simultaneously, then it is essential to understand the git version control system.

This documentation provides a quick setup process for Git and a few useful commands. We will start this tutorial by explaining how to setup git application. After that, we will check the git version and setup the user name and user email for the git account. Next, we will learn how to initialize a git repository for a project, check the git status, and setup a git remote repository for the project. Then, we will learn git push and pull commands and create new branches. Finally, we will understand the git log commands.

Git application setup

  • Download git application from https://git-scm.com
  • Install it on your computer.
  • Open the terminal (Mac) or Poweshell (Windows).
  • Once done, run the following command to check the git version.
git --version

Or

git --v

Output

Setup git user name and email

git config --global user.name "Your Name"
git config --global user.email "Your Email"

Validate git user information

git config --global user.name
git config --global user.email

Git initalization command

Go to a project directory and run the following command to initialize a git repository.

git init

Check git repository status

git status

Git add and commit command

git add -A

git commit -m "your_custom_commit_message_in_here"

You can also combine those two lines of commands into a single lie.

git commit -am "your custom_commit_message_in_here"

Setup the origin of remote repository

git remote add origin "git_remote_repository_url"

Check remote origin status

git remote -v

Set default upstream branch for push and pull

git push -u origin main

Git pull and push command

git push origin main
git pull origin main

Set a new URL for remote origin

git remote set-url origin "git_remote_repository_url"

Git Branch Commands

// List of all branches.
git branch -a

// Create a new branch and switch to that branch
git checkout -b "new_branch_name"

//Rename current branch.
git branch -m "new_renamed_branch_name"

// delete a branch.
git branch -d "deleted_branch_name"

//switch to a branch.
git checkout "branch_name"

// merge a branch.
git merge "branch_name"

git logs in oneline mode

git log --oneline

Output

git logs in oneline mode

git logs in graphical mode

git log --graph

Output

git logs in graphical mode

Last 3 git logs

git log -3
Last 3 git logs

Blog credit: https://github.com/xenioushk/useful-git-commands

,

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*

Back To Top