DEV Community

Aliya Lewis
Aliya Lewis

Posted on

27 5

Git it? Got it? Good.

Hey everyone! This will be a short and sweet blog on git commands. Let's git started (that was cringy for me too but I couldn't resist!).

Git init

This might be the simplest command. It creates a new repo and all you have to do is type this:

                  git init [repository name]

Git clone

This will to create a copy or "clone" of an existing repo. Just type:

                  git clone [url to the repo]

A handy shortcut that can be created is gcl, if you're into saving keystrokes check I'd suggest using it.

Git diff

Git diff shows the differences in files that have not yet been staged. To see them, all you have to type is "git diff".

Git diff --staged

This will show you the differences in the files that have been staged.

Git diff [branch1] [branch2]

Shows the differences between the specified branches.

Git reset

This will unstage a file but keep the contents the same.

git reset [file name]

Git reset [commit]

This command undoes all commits after the specified commit but changes are only saved locally.

Git reset -hard [commit]

Returns content back to the specified commit.

Git rm

Will delete the file from your working directory and stages it.

git rm [file]

Git remote

This command will connect your local repo to the remote server:

git remote add [variable name] [remote server link]

Example:

git remote add origin https://github.com/youracccount/repo.git

That's it for this time. Let me know what git commands you use often or think are important to know in the comments!!!

Resources:

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (8)

Collapse
 
cescquintero profile image
Francisco Quintero πŸ‡¨πŸ‡΄ β€’

Did not know git diff --staged existed.

Gonna try it next time.

Collapse
 
aliyalewis profile image
Aliya Lewis β€’

Glad I could help!

Collapse
 
yenyih profile image
Jordan Soo Yen Yih β€’

"git fetch" and "git merge origin branch_name" are best practice every time before you commit your codes. (Don't mess with your teammates)πŸ˜‰

  • Tips: "git fetch --prune", "--prune" is the best utility for cleaning outdated branches.
Collapse
 
aliyalewis profile image
Aliya Lewis β€’

Thanks! I didn't know about "git fetch --prune", I'll have to try that!

Collapse
 
qaispalekar profile image
Qais Palekar β€’

Did not know git diff branch1 branch2 existed
will come in handy. Thanks!!

Collapse
 
aliyalewis profile image
Aliya Lewis β€’

You're welcome!

Collapse
 
giongto35 profile image
giongto35 β€’

Good attempt writing on Git but I would suggest talking about "git add" and "git commit" over "git add", "git reset", "git remote" to suit beginners more.

Collapse
 
aliyalewis profile image
Aliya Lewis β€’ β€’ Edited

I didn't attempt to write about git commands, I did write about git commands. Also, I intentionally left those out as I felt they were commonly searched/used. But if any beginners are looking for the commands you mentioned, they can find them by clicking on the link under "resources" at the end of my blog πŸ™‚

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

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

Okay