DEV Community

Cover image for πŸ’‘ Useful Git Commands you need to know πŸ’‘
Kaiwalya Koparkar
Kaiwalya Koparkar

Posted on β€’ Edited on

19 5

πŸ’‘ Useful Git Commands you need to know πŸ’‘

Git workflow

  1. git init -> Initializes git in any folder/repository (Needs only if you are not cloning a repository)

  2. git clone https://github.com/<your-user-name>/<repo-name> -> Clones the repository in your local system.

  3. git status -> Shows the current status of the repository.

  4. git add <file-name> -> Adds specific file to staging area

  5. git diff / git whatchanged -> Gives the recent changes in the repository

  6. git add . -> Adds all changed files to staging area

  7. git commit -m "<your-message>" -> Gives a message to your current files and takes their snapshot to commit history

  8. git log -> Shows the commit history

  9. git revert <commit-token> -> Discards the specific commit (Deletes the committed files but keeps a trace in history)

  10. git reset --soft HEAD~<no-of-commits-to-revert> -> Undo's the commit and brings the changes back in the staging area

  11. git restore --staged <file> -> Brings back the specific file in the changes made section which is added to the staging area.

  12. git remote -v -> Shows all the remote connection

  13. git remote add origin https://github.com/<your-user-name>/<repo-name> -> adds your forked branch as the origin (No need to do if the repo is cloned)

  14. git remote add upstream https://github.com/<parent-user-name>/<repo-name> -> Adds parent repository as upstream.

  15. git pull origin -> fetches the changes made in origin to your local system

  16. git pull upstream -> fetches the changes made in origin to your local system

  17. git branch <branch-name> -> Creates a branch with branch-name

  18. git checkout <branch-name> -> This now allows you to make changes in the specified branch

  19. git checkout -b <branch-name> -> This is combination of git branch <branch-name> and git checkout <branch-name>

  20. git merge <branch-name> -> merges its children branch-name into its parent branch.

  21. git branch -d <branch-name> -> Deletes the specified branch. And if the changes in the branch-name are not merged in the parent branch then the changes are deleted.

  22. git push origin <branch-name> -> Pushes the recent commits to the new branch

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (10)

Collapse
 
r2d22 profile image
roman β€’ β€’ Edited

@kaiwalyakoparkar , thank you for your post.
I did not find also few commands but found the source :)

For everyone who want to find more information please use the link:
business-science.github.io/shiny-p...

Collapse
 
darthbob88 profile image
Raymond Price β€’ β€’ Edited

My current project at work requires a lot of little changes to build locally, changes which I can't push up to master.

My workaround is to create a branch with one commit containing those changes, git cherry-pick it into my current working environment, then git rebase -i to remove it from the changes I'm going to push.

Collapse
 
kaiwalyakoparkar profile image
Kaiwalya Koparkar β€’

Thanks for that information. I will do quick research on that commands and will sooner add them....πŸ˜€

Collapse
 
mjablecnik profile image
Martin JablečnΓ­k β€’

I am missing here: git rebase and git reflog..

Collapse
 
kaiwalyakoparkar profile image
Kaiwalya Koparkar β€’

Thanks for the reply. I will add it soon!

Collapse
 
samrocksc profile image
Sam Clark β€’

<3 this

Collapse
 
neontuts profile image
Neon Tuts β€’

It helped me a lot Thank You πŸ™

Collapse
 
kaiwalyakoparkar profile image
Kaiwalya Koparkar β€’

Glad to hear that!

Collapse
 
gash profile image
Gash β€’

I think one git stash and git stash pop are also one of the most important commands.

Collapse
 
kaiwalyakoparkar profile image
Kaiwalya Koparkar β€’

Yup, they are indeed but they are used occasionally when you have to save the changes temporarily somewhere else. This blog was aimed to provide a cheat sheet to the beginners and intermidiates. I am planning to write about these commands in the next blog. so stay tuned :)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay