Git Basics for Beginners

Git keeps a full history of a project as a chain of snapshots. Most confusion goes away once you know where a change sits at each step and which command moves it.

Where your work lives

A change in Git can sit in four places, and most commands move it from one to another.

Working tree
The ordinary files in your project folder.
Staging area (also called the index)
The exact contents that will go into the next commit. git add copies the current version of a file here.
Commits
Saved snapshots of the whole project, stored in the hidden .git folder. Each commit points to its parent, and that chain is the history.
Remotes
Other copies of the repository, usually on a server.

A branch is only a movable name that points at one commit. When you commit on a branch, the name moves forward to the new commit. HEAD means "where you are now", which is normally the branch you have switched to.

The staging area lets you build each commit on purpose: edit five files, stage the two that belong to one fix, and commit the rest separately. For remotes, git clone copies a repository, git fetch downloads new commits, git pull fetches and merges them, and git push uploads yours.

A short walkthrough with real output

This output came from Git 2.43 in an empty folder, with the identity "Example User" <user@example.com> and a shortened path. Your commit hashes will differ, because a commit hash also covers the author and the time.

$ git init -b main
Initialized empty Git repository in /home/user/demo/.git/
$ printf 'Hello\n' > notes.txt
$ git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        notes.txt

nothing added to commit but untracked files present (use "git add" to track)

Untracked means Git can see the file but has never stored it. Stage it and make the first commit:

$ git add notes.txt
$ git status --short
A  notes.txt
$ git commit -m "Add notes"
[main (root-commit) 46564f8] Add notes
 1 file changed, 1 insertion(+)
 create mode 100644 notes.txt

With no arguments, git diff shows edits in the working tree that are not staged yet. Add a second line and look:

$ printf 'Hello\nWorld\n' > notes.txt
$ git diff
diff --git a/notes.txt b/notes.txt
index e965047..f9264f7 100644
--- a/notes.txt
+++ b/notes.txt
@@ -1 +1,2 @@
 Hello
+World
$ git add notes.txt
$ git commit -m "Add second line"
[main 4d6d0a6] Add second line
 1 file changed, 1 insertion(+)
$ git log --oneline
4d6d0a6 Add second line
46564f8 Add notes

Next, create a branch, commit on it, then merge it back:

$ git branch feature
$ git branch
  feature
* main
$ git switch feature
Switched to branch 'feature'
$ printf 'Draft\n' > todo.txt
$ git add todo.txt
$ git commit -m "Add todo list"
[feature 53a5b8b] Add todo list
 1 file changed, 1 insertion(+)
 create mode 100644 todo.txt
$ git switch main
Switched to branch 'main'
$ git merge feature
Updating 4d6d0a6..53a5b8b
Fast-forward
 todo.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 todo.txt

That was a fast-forward: main had no new commits, so Git moved the name forward. If both branches had new commits, Git would make a merge commit with two parents, and stop for you to fix any conflicting lines by hand. Last, git restore throws away an unstaged edit:

$ echo junk >> notes.txt
$ git restore notes.txt
$ git status --short
$

What a commit hash is

Git names every stored object (file contents, directory listings and commits) by the SHA-1 hash of that object, written as 40 hexadecimal characters. Output usually shows a short unique prefix such as 46564f8.

File contents are stored as a blob. The hash covers a small header (the word blob, a space, the size in bytes and a NUL byte) followed by the content. You can reproduce it outside Git:

$ printf 'Hello\n' | git hash-object --stdin
e965047ad7c57865823c7d992b1d046ea66edf78

>>> import hashlib
>>> content = b"Hello\n"
>>> data = b"blob %d\x00" % len(content) + content
>>> data
b'blob 6\x00Hello\n'
>>> hashlib.sha1(data).hexdigest()
'e965047ad7c57865823c7d992b1d046ea66edf78'

The two values match, and they also match the index e965047..f9264f7 line in the diff above. A blob hash depends only on content, so it is the same on every machine. A commit hash also covers the parent hash, so editing an old commit changes every hash after it. If hexadecimal is unfamiliar, see number bases.

Ignoring files with .gitignore

A .gitignore file in the repository root lists patterns for untracked files that Git should leave alone. Commit it so everyone shares it.

node_modules/
*.log
!keep.log
/.env

A trailing slash matches folders only, a leading slash ties the pattern to the top level, and ! brings back a file an earlier pattern excluded. The most common surprise is that ignoring a file Git already tracks does nothing. Run git rm --cached <file> to stop tracking it while keeping it on disk, then commit that change.

Undo options side by side

SituationCommandEffect
Discard unstaged edits to a filegit restore <file>The edits are lost for good.
Unstage a file but keep the editsgit restore --staged <file>The working tree is untouched.
Fix the last commit message or add a forgotten filegit commit --amendReplaces the last commit (new hash).
Take back the last commit but keep its changesgit reset --soft HEAD~1The branch moves back; changes stay staged. Plain git reset HEAD~1 leaves them unstaged.
Throw away the last commit and its changesgit reset --hard HEAD~1Uncommitted work is lost too.
Undo a commit that is already pushedgit revert <commit>Adds a new commit that reverses it.

A simple way to remember them: restore works on files, reset moves a branch and rewrites your local history, and revert adds new history. Do not reset or --amend commits already pushed to a shared branch, because others may have built on them. If you reset too far, git reflog lists where HEAD has recently pointed, so committed work can usually be found again.