Home / Tips / Git basics: the commands that cover most days

Git basics: the commands that cover most days

Most days in a DevOps job need only a handful of Git commands. Here is the short list, and what each one is for.

Git tracks changes to files in a folder called a repository. You do not need to know its internals to use it well. You need a small set of commands, used in a steady rhythm.

The daily loop

  1. git status shows what changed and what is staged. Run it often.
  2. git add <file> stages the changes you want in the next commit.
  3. git commit -m "clear message" saves a snapshot with a message that says why.
  4. git log --oneline lists the history, one line per commit.

Branches

A branch is a line of work. Create one for each change so the main line stays clean.

git switch -c fix-login
# ...make changes, add and commit...
git switch main
git merge fix-login

git switch changes branch. The -c flag creates the branch first.

Sharing work

git pull brings in changes from the remote. git push sends your commits up. On a new branch, the first push needs git push -u origin fix-login so later pushes know where to go.

Undoing safely

  • Before a change is committed, git restore <file> throws away local edits to that file.
  • After a commit, git revert <commit> makes a new commit that undoes it. It keeps the history honest.
  • git reflog shows where HEAD has been, which is the way back from most mistakes.

Rule of thumb: never rewrite history that someone else has already pulled. Reverts are the safe choice on shared branches.

Next step

Want to practise this in a live batch?

We run these labs step by step.

WhatsApp us