DEV Community

Essential git commands

Shoukrey Tom on June 12, 2020

git init this command initializes the repository.git init git add this command adds file/files to the repository.git add file_name.ex...
Collapse
 
bravemaster619 profile image
bravemaster619

Here are more practical ones(IMHO):

  • Change the last commit message
    git commit --amend

  • Revert last unpushed commit
    git reset HEAD~1 --soft

  • Show log one line
    git log --oneline
    Press q to exit

  • Delete a local branch
    git branch -d <BRANCH NAME>
    Ignore push and merge status and force delete:
    git branch -D <BRANCH NAME>

  • Delete a remote branch
    git push <REPOSITORY REF> --delete <BRANCH NAME>
    example:
    git push origin --delete beta-1.0.0

    If branch ref is equal to a tag ref:
    git push <REPOSTIORY REF> :refs/heads/<BRANCH NAME>
    example:
    git push origin :refs/heads/beta-1.0.0

Collapse
 
iamphytan profile image
Damien LaRocque • Edited

And

  • Create a new branch and checkout at the same time

git checkout -b <new-branch-name>

Collapse
 
bravemaster619 profile image
bravemaster619

ty!

Collapse
 
rokokorag profile image
Rodrigo Acevedo

And

  • Get all log graphically

git log --all --graph --oneline

  • Get status summary

git status -s

Collapse
 
bobnudd profile image
Ash Grennan

Great list, if I was to add one:

git cherry-pick [commit id here] incredibly useful to pick specific commits when you don't want the branch.

Collapse
 
tarunvella profile image
tarunvella • Edited

It is always better to use git checkout [commit id here || branch name] [filename of other branch] than cherry pick

Collapse
 
gustavopch profile image
Gustavo

Not always better. They work differently:

  • cherry-pick will append the specified commit to the current one.
  • checkout will bring the specified commit's changes to the working directory (it's up to you to commit those changes).

It really depends on what you want to do. I use both frequently.

Collapse
 
abdulshakoor profile image
Shoukrey Tom • Edited

thank you

Collapse
 
iamphytan profile image
Damien LaRocque
  • git remote add <remote-name> <remote-url>
    Add remote to project

  • git remote rename <old-remote-name> <new-remote-name>
    Rename project remote

  • git remote rm <remote-name>
    Delete remote from project

Collapse
 
souravbadami profile image
Sourav Badami

Something related to git remote which helps in changing the existing remote URL for a remote.

git remote set-url <existing-remote-name> <new-remote-url>
Collapse
 
iamphytan profile image
Damien LaRocque

Yes, forgot this one
Thank you very much 🙏

Collapse
 
abdulshakoor profile image
Shoukrey Tom

great

Collapse
 
sainig profile image
Gaurav Saini

I find this extremely useful a lot of times
git reflog
Shows the history of the local HEAD. Useful when you lost some local code recently

Collapse
 
olalani profile image
Olalani Oluwaseun

I must sincerely say that I find this write up helpful. Thank you all