Git Commands

Basic Commands.

  • git init - Initializes a new Git repository in the current directory.

  • git clone [url] - Creates a copy of a remote repository in a local directory.

  • git remote add origin [url] - Creates a copy of a local directory in remote repository when we push code.

  • git status - Displays the state of the working directory and staging area.
  • git add [file] / . - Stages a file for the next commit.
  • git restore --staged [file] - To unstage a file.
  • git restore [file] - To Discard a unstaged file.

  • git commit -m "[message]" - Commits the staged changes with a descriptive message.
  • git pull - Fetches and integrates changes from a remote repository into the current branch.
  • git push - Uploads local commits to a remote repository.
  • git push origin [branch-name] - Usage: git push origin master This will push your local master branch to the master branch on the remote repository named origin.

  • git branch - Lists branches & show current branch.
  • git branch [new-branch-name] - Creates a new branch.
  • git branch -d [branch-name] - Deletes a branch.
  • git branch -M [new-branch-name] - Renames a branch.

  • git checkout [branch-name] - Switches to the specified branch.
  • git merge [branch-name] - Merges changes from the specified branch into the current branch.

  • git diff - Displays changes between commits, the working directory, and the staging area.

  • git stash - Temporarily saves changes that are not yet ready to be committed.
  • git stash push -m "your message here" - stash with message/name.

  • git stash list - To see a list of stashes you’ve made. output
$ git stash list
stash@{0}: WIP on Sachi: 2baf704 few changes in OldCabinPage
stash@{1}: WIP on Sachi: 2baf704 few changes in OldCabinPage

  • git stash apply - To apply the most recent stash and keep it in the stash list.
  • git stash apply stash@{1} - To apply a specific stash and keep it in the stash list.

  • git stash pop - To apply the most recent stash and remove it in the stash list.
  • git stash pop stash@{1} - To apply a specific stash and remove it in the stash list.

  • git stash drop - To remove a most recent stash from the list without applying it
  • git stash drop stash@{1} - To remove a specific stash from the list without applying it.

  • git stash clear - To remove all stashes

Configuration Commands.

  • git config --global user.name "[name]" - Sets the global username for commits.
  • git config --global user.email "[email]" - Sets the global email for commits.

  • git config user.name "[name]" - Sets the username for commits.
  • git config user.email "[email]" - Sets the email for commits.

  • git config --list - Lists all configuration settings.

Remote Commands.

  • git remote -v - Lists the remote repositories associated with the local repository.
  • git remote add [name] [url] - Adds a new remote repository.
  • git remote remove [name] - Removes a remote repository.

Advanced Commands.

  • git log - Shows the commit history.
  • git reset [file] - Unstages a file from the staging area.
  • git reset --hard - Resets the index and working directory to match the specified commit (destructive).
  • git revert [commit] - Creates a new commit that undoes the changes from a specified commit.
  • git rebase [branch-name] - Re-applies commits from the current branch onto another branch (often used to integrate changes from one branch into another).
  • git tag [tag-name] - Creates a new tag pointing to the current commit.

Bonus Commands.

  • ls - Lists all the files & folders in the current directory.
  • ls -a - Lists all the files & folders in the current directory including hidden files & folders.