Skip to main content

Basic Commands

List of Basic Commands Frequently Used

Frequently Used Git Commands

Below is a list of commonly used Git commands along with their descriptions, examples, and aliases (if available):

CommandDescriptionExampleAliases
git initInitialize a new Git repositorygit init
git clone [remote-url]Clone an existing repository from remotegit clone https://github.com/user/repo.gitcl
git statusShow the working directory status of modified filesgit statusst
git add [path of files]Add files to the staging areagit add filename1 filename2a
git add .Add all modified files to the staging areagit add .al
git commitCommit changes to the repositorygit commit -m "Proper / Meaningful Commit message"cm
git commit --amendUpdate or modify the very latest commit messagegit commit --amend
git push origin [branch-name]Push changes to a remote repository branchgit push origin mainpushoc
git pull origin [branch-name]Fetch and merge changes from a remote repositorygit pull origin mainpullob
git branchList, create, or delete branchesgit branchbr
git branch [branch-name]Create a new branchgit branch feature-branch
git checkoutSwitch branches or restore working tree filesgit checkout branch-nameco
git checkout -b [branch-name]Create and switch to a new branchgit checkout -b feature/NO-2311-SearchBarnb
git merge [branch-name]Merge a specified branch into the current branchgit merge branch-name
git logShow commit logsgit loglg
git diffShow changes between commits, commit and working tree, etc.git diff
git resetReset the current HEAD to the specified stategit reset --hard HEAD
git reset --hardDiscard all local changes and reset to the most recent commit historygit reset --hard
git rmRemove files from the working directory and staging areagit rm filename
git fetchDownload objects and refs from another repositorygit fetch origin
git remote add origin [repo-url]Add or set the remote URL for the local repositorygit remote add origin https://github.com/user/repo.git
git remote -vShow the current repository's remote URLgit remote -v
git remote set-url origin [url]Modify the remote URLgit remote set-url origin https://github.com/user/repo.git
git cherry-pick [commitId]Apply changes from a specific commit to the current branchgit cherry-pick eadbcedabf0b9a054dbecd96cfe008e76e46368a
git revert [commitId]Revert changes from a specific commit using its commit IDgit revert eadbcedabf0b9a054dbecd96cfe008e76e46368a

These commands are essential for day-to-day Git operations and can help streamline your workflow.

Temporarily stored in locally with modified, tracked files to change branches

Use case of below

After making some changes in files, and want to switch to different feature branch, with below commands save modified files and get back those modified files based on your needs. Below commands help you temporarily store your changes, allowing you to switch branches without losing your modifications.


CommandDescriptionExample
git stash save ["name"]Save temporarily modified file in name givengit stash save ["name"]
git stash listList all stashed changesgit stash list
git stash popApply the latest stashed changes and remove them from the stash listgit stash pop
git stash pop ["name"]Get modified files back with respect to feature branch and deleted from stash itemsgit stash pop ["name"]
git stash apply [stash@{n}]Apply a specific stash without removing it from the stash listgit stash apply stash@{0}
git stash apply ["name"]Get modified files back stash list & applied, but will not deleted from stash itemsgit stash apply ["name"]
git stash drop [stash@{n}]Delete / Remove a specific stash from the stash listgit stash drop stash@{0}
git stash clearDelete / Remove all stashed changes from the stash listgit stash clear
git stash save ["message"]Save your changes to the stash list with a messagegit stash save "message"
git stash dropDiscard the changes from top of stash stackgit stash drop

These commands cover the most frequently used operations in Git. Each command includes a brief description and an example to help you understand its usage.

Cherry Pick

git cherry-pick <commit-hash-1> <commit-hash-2> <commit-hash-3>
  • How to cherry pick a range of commits
git cherry-pick <commit-hash-1>^..<commit-hash-2>
  • How to cherry pick a commit from another branch
git cherry-pick <branch-name>/<commit-hash>
  • How to cherry pick a commit from another repository
git cherry-pick <repository-url>/<commit-hash>
  • How to cherry pick a commit from another repository and branch
git cherry-pick <repository-url>/<branch-name>/<commit-hash>

Git Rebase

While pushing the final feature branch to remotely, use squash to avoid multiple commits. Squash will make multiple commits into single commit

  1. Always rebase from master branch, make sure branch is up to date

    git rebase -i master
  2. You should see a list of commits, each commit starting with the word "pick”. Make sure the first commit says "pick" and change the rest from "pick" to "squash”. This will squash each commit into the previous commit, which will continue until every commit is squashed into the first commit. Save and close the editor. It will give you the opportunity to change the commit message. Save and close the editor again.

  3. Finally while pushing, append --force-with-lease

    git push origin feature_branch --force-with-lease