Top 10 most used git commands

Bindu Shrestha
3 min readOct 15, 2022

--

Following is the list of git commands that are used most frequently.

  1. git clone- Used to clone or copy existing repository. If you joined a new project today, this is the first command you’re going to use to create a local copy of the target repository.
    Ex: git clonehttps://github.com/bindushrestha/comparator-sample.git
    Clone from remote repository.
    git clonehttps://github.com/bindushrestha/comparator-sample.git dir1
    Clone to directory ‘dir1’.
  2. git init- Used to create a new git repository. If you create a new project this is the first command to run to turn it into a git project before you can run any other git command.
    Ex: git init
    The git init command creates a new Git repository.
  3. git branch- Used to list, create or delete branches.
    Ex: git branch -a
    List all the branches.
    git branch -d feature1
    Delete branch called feature.
    git branch -D feature1
    Force delete branch feature1.
    git branch feature2
    Create new branch feature2.
  4. git checkout- Used to switch branch, switch to a older commit, copy file, undo changes in a file.
    Ex: git checkout feature2
    Switch to branch feature2.
    git checkout feature1 -- a.txt
    Copy the file a.txt from feature1 branch to current branch.
    git checkout 4cb52c9e9744854
    Switch to a previous commit with the given commit id.
  5. git add- Used to add the changes made on current branch to staging area (can be thought of as a buffer, before the changes are actually saved to the repository). Files must be in staging area before they are actually saved using git commit.
    Ex: git add -a
    Adds all the modified files to the staging area.
    git add modifiedFile.txt
    Adds modifiedFile.txt to staging area.
    Note: You will always use ‘git add’ with ‘git status’ and ‘git commit’.
  6. git status- Used to see the status of working directory. Let’s you see which files are staged for commit, which are not and which files aren’t being tracked by git on the current working branch.
    Ex: git status
git status example

In above example a.txt is added to staging area withgit add a.txtand can be committed. b.txt is modified but not added to staging, when committing this won’t be saved to the repository. c.txt is a new file which is not tracked yet by git. You can also dogit add c.txtto add this new file to staging area and git will start tracking it.

7. git commit- Used to save the currently staged changes to the local repository. The changes are committed to the branch you are currently working on. Commits are snapshots of the entire repository at a specific point in time. It’s a good idea to always include commit message when making a commit so that when you check the history you know exactly what changes were made in a particular commit.
Ex: git commit -m “Add login feature”

8. git push- Used to transfer local commits to remote repository. After you make commits locally, you can share them with the remote repository using git push.
Ex:git push
Push the changes on current branch to corresponding branch on remote.
When pushing new branch you can use the following:
git push -u origin feature1
Creates new ‘feature1' branch in remote repository with tracking, so that from next push you can only usegit push .

9. git pull- Used to update the current branch with the changes in remote branch.
Ex:git pull origin feature1
Updates the currently checked out branch with the changes in the remote branch feature1.

If you do not mention any branch while doing git pull , it will update the the current branch and other remote tracking branches.

10. git stash — Used to save uncommitted changes. If you are in the middle of something, not yet ready to commit , you are going to need to save the task at hand before switching to something different. Git stash saves both staged and unstaged changes and reverts to the last commit.
Ex: git stash saves the changes in current branch.
git stash apply apply the saved changes to current branch.
git stash pop apply the saved changes to current branch and remove the stash.
git stash save "Add mail service" stash with message, useful in case you need to stash multiple times, to identify which stash you want to apply later.

--

--