Skip to content

Instantly share code, notes, and snippets.

@Kerry350
Last active April 23, 2020 04:23
Show Gist options
  • Save Kerry350/2043530 to your computer and use it in GitHub Desktop.
Save Kerry350/2043530 to your computer and use it in GitHub Desktop.
Common Git commands

A little lookup for commands I use frequently

  • Commit all edited files and add a message

git commit -a -m "My commit"

  • Add all new files

git add .

  • Perform a pull operation

git pull REMOTENAME BRANCHNAME

  • Perform a push operation

git push REMOTENAME BRANCHNAME

  • Prune all stale remote tracking branches

git remote prune REMOTENAME

  • Create a branch

git branch BRANCHNAME

  • View branches

git branch

  • Checkout a different branch

git checkout BRANCHNAME

  • Checkout a remote branch

git checkout -b LOCALBRANCHNAME origin/REMOTEBRANCHNAME

  • Merge the changes made in another branch in to the current branch

git merge BRANCHNAME

  • Delete a local branch

git branch -d BRANCHNAME

  • Delete a remote branch

git push origin :BRANCHNAME

  • Delete a remote branch (sexier syntax)

git push origin --delete BRANCHNAME

  • Scrap uncommitted state and return the working tree to the last committed state

git reset --hard HEAD

  • Delete the latest commit, and return to the one previous (one before HEAD)

git reset --hard HEAD~1

  • Return a single file to it's last committed state

git checkout -- FILENAME

git checkout HEAD FILENAME

  • Git log

git log

git log --pretty=oneline

git log --pretty=short

  • Cherry pick commits and apply them to another branch (first grab the commit ID from the branch with said commit, then checkout the branch you wish to apply the commit to)

git cherry-pick COMMIT-ID

  • Stash uncommitted changes

git stash save "message"

  • Apply stashed changes somewhere

git stash apply

  • Stop a file being tracked (but do not delete it from the working directory, add to .gitignore etc after this)

git rm --cached <file/folder>

  • Restore a file to a previous commit

git checkout <commitID> <file/to/restore>

  • Restore a file to one before a commit (say you know the commitID where something went wrong, and want one before that point)

git checkout <commitID>~1 <file/to/restore>

@kamal-agarahari-jabong
Copy link

It is really a nice article.Thanks a lot.

@nilahdad
Copy link

Thanks a lot...

@RababMojahed
Copy link

Thanks a lot

@balakrishnavalluri-gep
Copy link

Thanks bro nice work keep it up...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment