Skip to content

Instantly share code, notes, and snippets.

@dehghani-mehdi
Last active October 4, 2022 12:12
Show Gist options
  • Save dehghani-mehdi/bce335ddb02b0b050a722a778702dd01 to your computer and use it in GitHub Desktop.
Save dehghani-mehdi/bce335ddb02b0b050a722a778702dd01 to your computer and use it in GitHub Desktop.
Useful git commands
  • Pull (Update repo): git pull
  • Commit:
    • git add .
    • git commit -m "COMMIT COMMENT"
    • Short version: git commit -am "COMMIT COMMENT"
  • Push (Update remote): git push <remote-name> <branch-name>
  • Clone: git clone <url>
  • Clone a branch: git clone <url> BRANCH_NAME
  • Clone and create a branch: git clone <url> -b BRANCH_NAME
  • Get repo's remote URL: git config --get remote.origin.url
  • Update repo's remote URL: git remote set-url origin <url>
  • See changed files before commit (differences between current commit and working tree): git diff HEAD or git diff [filename]
  • Undo the most recent commit: git reset --hard HEAD~1 or git reset HEAD~1 (--hard flag reset your files to their state at prev commit, more info)
  • List changed files in most recent commit: git show --stat HEAD (more info)
  • Clone a subdirectory only of a Git repository: (Using a git-bash prompt NOT powershell or CMD)
    • git init
    • git remote add origin <url>
    • git config core.sparsecheckout true
    • echo "DIR_NAME/*" >> .git/info/sparse-checkout or echo "DIR_NAME" >> .git/info/sparse-checkout
    • git pull --depth=1 origin master
  • Init git in existing directory: (more info)
    • cd <localdir>
    • git init
    • git add .
    • git commit -m "🎉 init"
    • git remote add origin <url>
    • git push -u origin master
  • Commit count: git rev-list --count <revision>, e.g: git rev-list --count main
  • Update the local list of remote branches: git remote update origin --prune
  • Show all local and remote branches that (local) Git knows about: git branch -a
  • Undo pushed commit
    • Undo and create new commit: git revert <commit_hash> then git push
    • Remove commit form the history: git reset <commit_hash> then git push -f
  • Proxy more info
    • git config --global http.proxy address:port
    • git config --global http.proxy http://user:password@address:port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment