Sitemap
Utkarsh Singh

Utkarsh Singh

Software developer

All Basic Git Commands You Should Know

4 min readJan 20, 2024

Git is a distributed version control system used for tracking changes in source code during software development. It enables collaboration, branching, merging, and maintains a history of changes. Developers use Git to manage and version their code efficiently. Platforms like GitHub, GitLab, and Bitbucket provide hosting services for Git repositories.

Press enter or click to view image in full size

There are many blogs on Git but the basic commands which we all need as a developer are not mentioned in a single blog. So in this blog all of the basic git commands will be described.

    Get Utkarsh Singh’s stories in your inbox

    Join Medium for free to get updates from this writer.

  1. git config : This command is used to configure settings and customize the behavior of the Git tool.
    git config — global user.name “Your Name”
    git config — global user.email “your.email@example.com”
    by using this commands you can set username and the user email for the remote repository where you wanted to push your code.
    git config — list
    by using the command you can see the current username and email which is set for your system.
  2. git init : This command is used to initialize the repository in you local system. If you have cloned any repository from the github it will be consisting the .git file which is hidden by default.
  3. git remote add origin : This command is used to add the local repository to the remote repository. git remote add origin <remote_repository_URL> where the <remote_repository_URL> is your repository url which you wanted to connect.
  4. git add : This command is used to add the changes made by you in your local system. If you use git add . command it will add all the changes. Either if you want to add only changes done in specific files you can use git add <file_name> this command. This is also know as staging the changes because your changes will be staged to commit.
  5. git commit : This command is used to make the staged changes save in your local repository such that they can be pushed to remote repository. The command is git commit -m <Your commit message here> you can write your own message based on the changes you did.
  6. git push : This command is used to push the changes which are commited in your local repository to the remote repository. git push origin <branch_name> command is used to push the specific branch to the remote repository. If you want to set the brach from where you will always push to the remote then u can use git push -u origin <branch_name>. “-u” sets the current repository to use for global.
  7. git status : This command is used to know the current status of the local and remote repository. It will show the untracked and modified changes which are not pushed to the remote.
  8. git clone : The git clone command is used to clone a remote repository to you local system such that you can edit what you want and can push to the remote. git clone <repository_url> is used where the repository url is the url for the remote repository which you want to clone.
  9. git log : The git log command is used to log all the previous commit you have done you can track of all by the commit hash.
  10. git pull : This command is used to pull from the main remote repository which have been changes by some other branch or from previous commit from the same branch. If you wanted to pull from specific branch you can use git pull origin <branch_name>. git pull — rebase is used when you want your local changes to be on top of the changes which are fetched instead of merging.
  11. git branch : To check branches the git branch command is used, to create a new branch we use git checkout -b <new-branch>, to move from a branch to another branch we use git checkout <branch_name>, to change the name of the current branch we use git branch -M <branch_name_to_give>, to delete a branch git checkout -d “branch-name” command is used.
  12. git merge : To merge the current branch with another branch we use git merge <branch_name>. The git diff <branch_name> command is used to know the difference between the two branch the current and the named.
  13. git reset : The git reset command is used to undo the changes we use get reset to undo the staged changes which are not commited yet, and for resting to the previous commit we use git reset HEAD~1 and for going to the last known commit we use the commit hash of that commit as git reset <commit-hash>. The reset can be of three types soft,mixed and hard. The soft reset only moves the head of the branch to that commit but it leaves the changes staged, the mixed reset not only moves the head to the branch also clears the staged changes and in hard reset it moves the head to the branch commit and discards all changes in the working directory and staging area.

--

--