Select Page

Here are some frequently used GIT commands.

Getting & Creating Projects

  • git init // Initialize a local Git repository
  • git clone ssh://[email protected]/[username]/[repository-name].git // Create a local copy of a remote repository  

Basic Snapshotting

  • git status // Check status git add [file-name.txt] // Add a file to the staging area
  • git add -A // Add all new and changed files to the staging area
  • git commit -m “[commit message]” // Commit changes
  • git rm -r [file-name.txt] // Remove a file (or folder)  

Branching & Merging

  • git branch // List branches (the asterisk denotes the current branch)
  • git branch -a // List all branches (local and remote)
  • git branch [branch name] // Create a new branch
  • git branch -d [branch name] // Delete a branch
  • git push origin –delete [branchName] // Delete a remote branch
  • git checkout -b [branch name] // Create a new branch and switch to it
  • git checkout -b [branch name] origin/[branch name] // Clone a remote branch and switch to it
  • git checkout [branch name] // Switch to a branch
  • git checkout – // Switch to the branch last checked out
  • git checkout — [file-name.txt] // Discard changes to a file
  • git merge [branch name]  // Merge a branch into the active branch
  • git merge [source branch] [target branch] // Merge a branch into a target branch
  • git stash // Stash changes in a dirty working directory
  • git stash clear // Remove all stashed entries  

Sharing & Updating Projects

  • git push origin [branch name] // Push a branch to your remote repository
  • git push -u origin [branch name] // Push changes to the remote repository (and remember the branch)
  • git push // Push changes to the remote repository (remembered branch)
  • git push origin –delete [branch name] // Delete a remote branch
  • git pull // Update local repository to the newest commit
  • git pull origin [branch name] // Pull changes from the remote repository
  • git remote add origin ssh://[email protected]/[username]/[repository-name].git // Add a remote repository
  • git remote set-url origin ssh://[email protected]/[username]/[repository-name].git // Set a repository’s origin branch to SSH  

Inspection & Comparison

  • git log // View changes
  • git log –summary // View changes (detailed)
  • git diff [source branch] [target branch] // Preview changes before merging  

Push a branch to master branch or other

  • git push origin puneet:master

Forcefully get the repository version of the branch:

If you have any local changes, they will be lost. With or without –hard option, any local commits that haven’t been pushed will be lost.

  • git fetch –all     // downloads the latest from remote without trying to merge or rebase anything.
  • git reset –hard origin/master    // resets local branch to repo forcefully deleting all local changes.
  • git reset –hard origin/<branch_name>    // command for other branch    

Saving current commits in a new local branch before resetting:

You can save current local commits before resetting by creating a local branch with the commits.

  • git checkout master
  • git branch new-branch-to-save-current-commits
  • git fetch –all
  • git reset –hard origin/master    

Git Stash – saving uncommitted changes:

This saves uncommitted changes and you can reapply them later on.

git stash     // saves uncommitted changes git stash pop // re-apply uncommitted changes     ————– Will be adding more commands ———————–