DEV Community

Shoukrey Tom
Shoukrey Tom

Posted on

Essential git commands

  1. git init

    this command initializes the repository.
    git init

  2. git add

    this command adds file/files to the repository.
    git add file_name.ext
    or add all files in one go: git add ., git add --all

  3. git commit

    this command commits the changes to the repository with a message.
    git commit -m "message"

  4. git clone

    this command clones a repository which is stored in a cloud (e.g: github) and creates a local copy.
    git clone url_for_repository

  5. git push

    this command pushes the local repository to the cloud(e.g: github).
    git push , git push -u remote_name branch_name

  6. git pull

    this command pulls or gets an changes from the copy of cloud-stored repository. git pull

  7. git branch

    this command creates/deletes a branch.
    Create: git branch branch_name
    Delete: git branch -d branch_name

  8. git rm

    this command is used to delete file/files from git repository.
    git rm --cached file_name.ext , git rm --cached .

  9. git status

    this command is used to show changes to the git repository(new files, modified files, deleted files).
    git status

  10. git log

    this command is used to show all commits.
    git log

  11. git checkout

    this command is used to switch between branches.
    git switch branch_name

  12. git merge

    this command is used to merge tow branches.
    git merge target_branch source_branch

Top comments (14)

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