Sign in Get started
Solutions
ComplianceDigital ForensicsAccount SecurityWebsite SecurityWeb OptimizationSEO / GEOBusiness IntelligenceUI / UXStartupsDigital TransformationWhite LabelAI Code AuditAI / MLConsulting & AdvisoryBusiness Strategy
Products
Web HostingVPS HostingSSL CertificatesSiteLock SecurityWebsite BackupEmail Spam FilterOX App Suite360 MonitoringWeebly BuilderSite BuilderSEO (Marketgoo)Account SecurityDomainsAll Products
More
NetworkSecurity AboutCareersContactRoadmapLegal
Guide

Git essentials: the commands you use every day

Git has well over a hundred commands, and most developers use about a dozen of them. This guide covers that dozen, a branching routine that keeps work separate, the safe ways to undo a mistake, and the graphical clients worth trying. The official reference is the free Pro Git book on git-scm.com.

Set up once

Tell Git who you are. Every commit records this name and e-mail. The last line makes new repositories start on a branch called main.

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main

The commands you use every day

How a change moves through GitWorking treeFiles you editon diskStaging areagit add picks whatgoes in the commitLocal repogit commit saves asnapshot with a noteRemotegit push shares;git pull fetchesWorking treeFiles you edit on diskStaging areagit add picks what goes in the commitLocal repogit commit saves a snapshot with a noteRemotegit push shares; git pull fetches
Each command moves changes one step to the right; restore and reset move them back.

Git keeps three places on your machine: the files you edit (the working tree), the staging area where you collect the changes for the next commit, and the repository with its history. A remote, such as a repository on GitHub, GitLab or your own server, is a fourth copy you share with others.

git clone https://example.com/team/app.git   # copy a repository
git status                                   # what changed
git diff                                     # line-by-line changes
git add src/app.js                           # stage one file
git commit -m "Fix login redirect"           # save a snapshot
git log --oneline --graph                    # history
git pull                                     # get others' work
git push                                     # share yours

A few habits make these commands work better:

  • Run git status before and after anything you are unsure about. It also suggests the next command.
  • Stage files by name, or review with git add -p, which shows each change and asks whether to include it. This keeps stray files out of commits.
  • Write commit messages that say what changed and why, in one short line. Add detail on following lines if needed.
  • Pull before you start work and before you push, so you deal with other people's changes early.
  • Keep secrets out of the repository. Put them in a file listed in .gitignore. A password that reaches a commit stays in history, so change it.

Branching

A branch is a movable pointer to a line of commits. Use one per task. You can then switch between tasks, review them separately, and drop a branch that went nowhere.

git switch -c fix/login-redirect   # new branch from where you are
# ...edit, add, commit...
git push -u origin fix/login-redirect
git switch main
git pull
git merge fix/login-redirect
git branch -d fix/login-redirect

git switch and git restore replaced the older, overloaded git checkout for these jobs in Git 2.23. checkout still works, and you will see it in older guides.

When a merge reports a conflict, Git marks the lines in each file with <<<<<<<, ======= and >>>>>>>. Edit the file to the version you want, remove the markers, then git add the file and git commit. git merge --abort returns to where you were before the merge.

To switch tasks with unfinished edits, park them first:

git stash          # park unfinished edits
git switch main
git stash pop      # bring them back

Undoing safely

Three ways to undogit restoreThrows away edits orunstages a filegit revertNew commit that undoesan old one; safe to pushgit resetMoves the branch back;only for unpushed workgit restoreThrows away edits orunstages a filegit revertNew commit that undoesan old one; safe to pushgit resetMoves the branch back;only for unpushed work
Pick the undo by where the change is now.

The right undo depends on where the change is. Work from the least drastic option.

Edits you have not committed. git restore resets a file to its last committed state. With --staged it only takes the file out of the staging area. git commit --amend replaces the last commit, which is fine before you push.

git restore src/app.js            # discard edits to a file
git restore --staged src/app.js   # unstage, keep the edits
git commit --amend                # fix the last commit message or add a file

A commit you have already pushed. Use git revert. It adds a new commit that does the opposite of the old one. History stays intact, so nobody else's copy breaks.

git revert 3f2a9c1    # new commit that undoes 3f2a9c1
git push

Local commits you want to drop. git reset moves your branch back. --soft keeps the changes staged, the default keeps them as edits, and --hard deletes them. Do not reset commits other people already have, because it rewrites shared history.

git reset --soft HEAD~1   # undo the last commit, keep changes staged
git reset HEAD~1          # undo it, keep changes unstaged
git reset --hard HEAD~1   # undo it and delete the changes
git reflog                # find a commit you lost

If you reset too far, git reflog lists every commit your branch has pointed to recently. Find the one you want and run git reset --hard with its ID to get back.

Graphical clients

A visual client helps with history, conflicts and staging parts of a file. All three below work with any Git host.

  • VS Code has Git built in. The Source Control view stages, commits, shows diffs, and has a merge editor for conflicts. It is free on Windows, macOS and Linux.
  • GitHub Desktop is a free, open-source client for Windows and macOS. It is simplest with GitHub repositories but works with others.
  • GitKraken Desktop runs on Windows, macOS and Linux and is known for its commit graph. The free plan covers local repositories and public remotes; private remote repositories need a paid plan.

Learn the command line too. Every client runs the same Git underneath, and the terminal is what you have on a server. If you develop on Windows, set up a Linux terminal with WSL and install Git inside it.

Common questions

What is the difference between git fetch and git pull?
fetch downloads new commits from the remote and changes nothing else. pull does a fetch and then merges, or rebases, those commits into your current branch.
Should I merge or rebase?
Both work. Merge keeps history exactly as it happened. Rebase rewrites your own branch on top of the latest main for a straight history. Never rebase commits others already use.
I committed a password. What now?
Change the password first. Then remove it from the code and commit. Removing it from history needs a tool such as git filter-repo and a force push that everyone must follow.
How do I see who changed a line?
git blame with the file name shows the last commit for every line. Most clients show the same in their history view.

Shipping code for clients? Getting paid on Internetivo explains how developers find paid tasks and get paid for accepted work.