Skip to content

Git Commands

Basic Commands

  1. Initializes a new Git repository in the current directory.

    git init
    

  2. Creates a copy of a remote repository in a local directory.

    git clone [url]
    

  3. Creates a copy of a local directory in remote repository when we push code.

    git remote add origin [url]
    

  4. Displays the state of the working directory and staging area.

    git status
    

  5. Stages a file for the next commit.

    git add [file] / .
    

  6. To unstage a file.

    git restore --staged [file]
    

  7. To Discard a unstaged file.

    git restore [file]
    

  8. Commits the staged changes with a descriptive message.

    git commit -m "[message]"
    

  9. Fetches and integrates changes from a remote repository into the current branch.

    git pull
    

  10. Uploads local commits to a remote repository.

    git push
    

  11. Usage: git push origin master This will push your local master branch to the master branch on the remote repository named origin.

    git push origin [branch-name]
    

  12. Lists branches & show current branch.

    git branch
    

  13. Creates a new branch.

    git branch [new-branch-name]
    

  14. Deletes a branch.

    git branch -d [branch-name]
    

  15. Renames a branch.

    git branch -M [new-branch-name]
    

  16. Switches to the specified branch.

    git checkout [branch-name]
    

  17. Merges changes from the specified branch into the current branch.

    git merge [branch-name]
    

  18. Displays changes between commits, the working directory, and the staging area.

    git diff
    

  19. Temporarily saves changes that are not yet ready to be committed.

    git stash
    

  20. stash with message/name.

    git stash push -m "your message here"
    

  21. To see a list of stashes you’ve made.

    git stash list
    
    output
    $ git stash list
    stash@{0}: WIP on Sachi: 2baf704 few changes in OldCabinPage
    stash@{1}: WIP on Sachi: 2baf704 few changes in OldCabinPage
    

  22. To apply the most recent stash and keep it in the stash list.

    git stash apply
    

  23. To apply a specific stash and keep it in the stash list.

    git stash apply stash@{1}
    

  24. To apply the most recent stash and remove it in the stash list.

    git stash pop
    

  25. To apply a specific stash and remove it in the stash list.

    git stash pop stash@{1}
    

  26. To remove a most recent stash from the list without applying it

    git stash drop
    

  27. To remove a specific stash from the list without applying it.

    git stash drop stash@{1}
    

  28. To remove all stashes

    git stash clear
    

Configuration Commands

  1. Sets the global username for commits.

    git config --global user.name "[name]"
    

  2. Sets the global email for commits.

    git config --global user.email "[email]"
    

  3. Sets the username for commits.

    git config user.name "[name]"
    

  4. Sets the email for commits.

    git config user.email "[email]"
    

  5. Lists all configuration settings.

    git config --list
    

Remote Commands

  1. Lists the remote repositories associated with the local repository.

    git remote -v
    

  2. Adds a new remote repository.

    git remote add [name] [url]
    

  3. Removes a remote repository.

    git remote remove [name]
    

Advanced Commands

  1. Shows the commit history.

    git log
    

  2. Unstages a file from the staging area.

    git reset [file]
    

  3. Resets the index and working directory to match the specified commit (destructive).

    git reset --hard
    

  4. Creates a new commit that undoes the changes from a specified commit.

    git revert [commit]
    

  5. Re-applies commits from the current branch onto another branch (often used to integrate changes from one branch into another).

    git rebase [branch-name]
    

  6. Creates a new tag pointing to the current commit.

    git tag [tag-name]
    

Other Commands

  1. Shows Present Working Directory.

    pwd
    

  2. Lists all the files & folders in the current directory.

    ls
    

  3. Lists all the files & folders in the current directory including hidden files & folders.

    ls -a