Course contents · Lesson 9 of 17

Git Basics: Commit, Push and Pull, Hands-On

Git and Collaboration · Lesson 9 of 17 · 12 min Engineer

You committed a bug fix last night, yet this morning your teammate cannot see it: the commit is saved, but only on your laptop. This lesson covers the Git basics of teamwork: committing a change, sending it to the shared copy with push, bringing others' work down with pull, and the usual first mistakes.

Commit: stage the change, then save it

As What Is Git? showed, Git builds each commit in the staging area, the waiting room between your edited files and the saved history. So saving takes two commands: git add stages a file, and git commit saves everything staged as one commit in your local repository.

git add contact.html
git commit -m "Add contact page so customers can reach us"

The -m option gives the message. Git replies with the branch, the short hash and the message, then a summary of the change; your hash will differ:

[main c92b5a0] Add contact page so customers can reach us
 1 file changed, 12 insertions(+)
 create mode 100644 contact.html

Git already records what changed; the message should say why: "Round prices down so totals match receipts" beats "Update file". Git's documentation suggests a first line of at most 50 characters.

Remotes: the shared copy called origin

A remote is a name your repository keeps for another copy of the project, usually on a server such as GitHub or GitLab. Cloning creates a remote called origin that points at the address you cloned from. To list your remotes with their addresses (-v means verbose), run:

git remote -v

Each remote appears twice, once for fetch (downloading) and once for push (uploading), usually with the same address, as in this example:

origin	https://github.com/asha/shop.git (fetch)
origin	https://github.com/asha/shop.git (push)

No output means no remote yet, which is normal after git init. To add one, run git remote add origin followed by the address your hosting site shows.

Push and pull: send your commits, bring theirs down

In the flowchart, everything inside the dashed box stays on your laptop. Only git push crosses to the shared copy, and git pull comes back.

Flowchart. Inside a dashed box labelled your laptop, private until you push: 1, working folder, you edit files; an arrow labelled git add leads to 2, staging area, the next snapshot; an arrow labelled git commit leads to 3, local repository, your commits. An arrow labelled git push leaves the box and reaches 4, the remote named origin, for example on GitHub, the shared copy. A return arrow labelled git pull, fetch then merge, runs from the remote back into the local repository and on to the working folder, labelled your files update too. A banner says commit saves locally, push shares, pull brings others' work
Nothing leaves your laptop until git push, and git pull brings others' work back.

git push uploads the commits on your branch that origin lacks and moves origin's branch to your newest commit. In a clone, main already has an upstream, the remote branch it is linked to, so a plain push knows where to go:

git push

After progress lines, it shows origin's main moving from the old commit to yours (trimmed, example hashes):

To https://github.com/asha/shop.git
   8d40e17..c92b5a0  main -> main

In a repository you made with git init, the first push fails with The current branch main has no upstream branch (or your branch's name). Run git push -u origin main once: -u creates that link for later pushes and pulls.

git pull first runs git fetch, which downloads new commits from origin without touching your files, then brings them into your branch. If you have no new commits of your own, that is a fast-forward: Git just moves main forward and updates your files.

git pull

After the download lines, a fast-forward looks like this, followed by a list of the files that changed (both trimmed):

Updating c92b5a0..e5f6a7b
Fast-forward

In the diagram, follow the numbers: each commit is made on one laptop, goes up with a push and reaches the other laptop only when that person pulls.

Three columns: Asha's laptop, the remote named origin, and Ravi's laptop. Asha makes commit 1. Step 1, Asha's git push sends commit 1 to origin. Step 2, Ravi's git pull brings commit 1 down to his laptop. Ravi makes commit 2. Step 3, Ravi's git push sends commit 2 to origin. Step 4, Asha's git pull brings commit 2 down to her laptop. At the bottom, all three copies hold commits 1 and 2, and a banner says after step 4, all three copies match
A commit reaches a teammate only after a push and then a pull.

Where am I? git status, git log and .gitignore

git status lists what you changed, what is staged and how your branch compares with origin/main. That is a remote-tracking branch: your repository's record of where main was on origin at your last fetch, pull or push. Status never asks the server, so run git fetch first for fresh news.

git status

Here the contact page is committed but not pushed, and a new notes file was never added (hint lines trimmed):

On branch main
Your branch is ahead of 'origin/main' by 1 commit.

Untracked files:
	notes.txt

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

Untracked means Git sees the file but has never stored it. To see the same picture as history, newest first and one line per commit, run:

git log --oneline

Each line holds the short hash, the names pointing there and the message. main sits on your new commit and origin/main on the one before; origin/HEAD marks the remote's default branch:

c92b5a0 (HEAD -> main) Add contact page so customers can reach us
8d40e17 (origin/main, origin/HEAD) Fix typo in title
3f1c2ab Add home page

Some files must never be tracked: secrets, downloaded libraries, build output and logs. A .gitignore file at the top of the repository lists them as patterns. Commit it so the team shares the list:

# secrets and local settings
.env
# downloaded libraries and build output
node_modules/
dist/
*.log

# starts a comment, and .env is a common name for a file of local secrets. A trailing / matches folders only, and * matches anything except /, so *.log covers log files in every folder.

One catch: .gitignore does not affect files Git already tracks. git rm --cached with the file's name stops tracking it but keeps it on disk; then commit.

Common stumbles: rejected push, forgotten add, leaked secret

Pushing before pulling

Ravi pushed while you were committing. Your push is refused, because accepting it would move origin's main to your commit and drop his commit (trimmed):

 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/asha/shop.git'

The fix is to pull, then push, and the same goes for (non-fast-forward). But both sides now have new commits, so the histories have diverged, and unless you have set a preference, recent Git versions stop git pull with fatal: Need to specify how to reconcile divergent branches. Choose merging once, then retry:

git config --global pull.rebase false
git pull
git push

Now git pull makes a merge commit, a commit with two parents that joins your work and Ravi's, and may open your editor with a ready-made message: save and close it. If you both changed the same lines, you get a merge conflict instead, which a later lesson resolves.

In the diagram, origin/main moves only when your repository talks to origin; after the pull and push, both names sit on the merge commit.

Three panels of commits drawn as circles, with two name tags: main, your branch, and origin/main, the remote's main as last seen. Panel 1, you commit C: commits A, B, C in a row, main on C and origin/main on B, status ahead by 1. Panel 2, git pull fetches D: after B the history splits into C, yours, carrying main, and D, Ravi's, carrying origin/main, status diverged, 1 and 1. Panel 3, merge into M, then push: C and D both lead to M, which has two parents, and main and origin/main both sit on M, status up to date. A banner says after pull and push, both names point at M
New commits on either side pull the two names apart; pull, then push, rejoins them.

Forgetting to stage

Edit a file and commit without git add, and Git saves nothing, ending with no changes added to commit. Run git add, then commit again.

Committing a secret

Deleting a password or API key in a later commit does not remove it: the earlier commit still holds it. If it sits in a file such as .env and only in your last, unpushed commit, run git rm --cached .env, list the file in .gitignore and run git commit --amend --no-edit to replace that commit.

Once pushed, treat the secret as leaked: revoke it and create a new one, as GitHub's guide to removing sensitive data advises.

Check yourself

Your teammate's git pull does not bring the fix you committed an hour ago. What did you skip?

git push. A commit lives only in your local repository, and the remote your teammate pulls from gets it only when you push.

git status says you are up to date, but a teammate pushed five minutes ago. How can both be true?

Status compares with origin/main, your record from the last fetch, pull or push, and never asks the server. git fetch updates that record.

You add .env to .gitignore, but git status still shows changes to it. Why?

.gitignore ignores only untracked files, and .env is already tracked. git rm --cached .env untracks it but keeps the file; commit that, and revoke its secrets if it was ever pushed.