Take Your Skills To The Next Level

More

Git Commands – Cheat Sheet for All Developers

Git Commands – Cheat Sheet for All Developers

Git has powerful commands that can save you time, effort, and even revert mistakes 😀 It's a great tool for all developers, but some commands are easily forgotten or unknown for junior developers.

Here is a comprehensive recap of the Git commands you might need in your everyday developer life. I’ve added complementary links if you want to dig deeper or need specific support for your problem.

First Steps: Create & Synchronize

While Coding: Inspect, Add & Commit

Ready to Push! 

Shortcuts

Create, Clone & Synchronize A Repository

—> Create a new repository on GitHub

Copy the SSH KEY available on Github once the repo is created (What is a SSH Key?)

On your terminal, type the commands:

git clone {SSH URL}

Great! Now you can work from your terminal and add/commit/push your work!

git init {repository name}

Make sure to create it on the path you want! You can also create it this way:

git init /home/code/personal-projects/{repository name}

git remote add origin {SSH URL}
git fetch
git push origin master

You need to clone a repository from Github 

From your Terminal:

git clone{SSH URL}

OR

git clone{HTTPS URL}

Where can I find the SSH Url and the HTTPS Url?

GIT1

The difference between SSH Key and HTTPS URL

git pull 

→ Update your local branch with what your colleagues pushed to Github since your last git pull

Best practices: Run git pull first thing everyday! Right before starting a new branch or continuing on one. It will update your branch with the latest push(es) your colleagues have been working on.

Create & Change branch

Create a new branch on your master branch and switch to it:

git checkout -branch {your-branch} 

—> Checkout allows you to switch from one branch to another

Switch to a branch:

git checkout {your-branch} 

Switch to the master/main branch:

git checkout master 

New command in town!

git switch {your-branch}

→ Alternative for checkout – More about git switch

Best practices: Remember to close and delete the branch once your pull request is merged (on GitHub). No worries, a branch can always be restored if needed 😉

Next time you want to go back to an open branch from your terminal (using tab because you can’t remember the exact name of it), you will avoid overwhelming lines of suggestions.

Browse & Inspect your projects/branches

git log 

→ See each commit of the current branch with the name of the author, the date the commit message and its commit key.

git diff 

git diff {branch1}...{branch2}

git status 

Best practices: Use both git status and git diff before git add/git commit/git push, to double check if you edited a file/line by mistake – it’s way easier than checking the files in the codebase!

Add

git add {folder or files or .}

→ means everything in the repository – be careful when you use it! It’s safer to add the folders you worked on. 

Git add is the first step to save, as a snapshot of your current work. It usually follows with git commit and git push (cf ‘Ready to push!’ section). More about Saving changes.

Best practices: When git add-ing, you can use git status before, to see what files have been changed. So you are sure to snapshot the exact files you want 😉

e.g.

git status
## your-branch
app/styles/component/profile/profile.scss

→ The file that has been updated is from the app folder,so you can be more specific when you save and write:

git add app

If you want to add the specific file, hit tab and add the path you need to reach it

git add app/styles/component/profile/profile.scss

Commit

git commit -m "your message"

git checkout {your branch}

git reset {commitkey} 

→ preserves local changes

OR

git reset --hard {commit key} 

—> (changes back to the specific commit) 

git log 

—> Check if your latest history log is the commit you wanted to go back to

git push --force-with-lease origin {your branch}

Done! 

More about: Revert a commit, Git reset, Force with lease

git checkout {your-branch}
gitrebase -i master

On the git rebase todo, replace {pick} with {squash} or {s} – it will use the commit, but meld it into the previous commit.

Enter until you are back into your terminal.

Then:

git push --force-with-lease origin {your-branch}

You’re done! Your commits’ history should be updated 🙂

Git rebase is quite powerful, you can also use commands such as:
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]

If you want interactive git rebase, check out this article!

More about: Git rebase

READY TO PUSH!

Add, commit and push

Did you inspect your code and files with git status AND/OR git diff? If not, I recommend you to go back to ‘Browse & Inspect’!

git add {folder OR file OR .} 

→ . means everything in the repository, be careful when you use it! It’s safer to add the folders you worked on

git commit -m "your message" 

How to write a good commit message?

You can also combine git add and commit in one line:

git add {folder} && git commit -m "your message"

git push origin {your-branch}

Git commands are not that difficult to grasp, but we tend to forget the syntax, the meaning, or the word we need to do a command. I hope this article will help you in the first steps of your developer career!

Interested in more git commands? Great! I’ve regrouped additional readings to complete your git skills :muscle:

Think Like (a) Git – For advanced reading

SHORTCUTS

-branch → -b

git checkout -b {new-branch}

checkout → co

git co master

status → st

git st

-force → -f

git push -f origin {your-branch}

Git rebase commands:

pick → p
reword → r
squash → s
fixup → f
exec → x
break → b
drop → d
label → l
reset → t
merge → m

Related posts
More

Mastering the JavaScript change Assertion — Dutfe

More

Getting Began with HTML Tables — Dutfe

More

404: Not discovered – Dutfe

More

404: Not discovered – Dutfe

Sign up for our Newsletter and
stay informed

Leave a Reply

Your email address will not be published. Required fields are marked *