DEV Community

Aliya Lewis
Aliya Lewis

Posted on

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:

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 πŸ™‚