#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Version Control Basics: Commit, Push, and Pull

In Git, commit saves a snapshot of your staged changes in your local repository, push uploads your new commits to a remote such as GitHub, and pull downloads other people's commits and merges them into your branch. This guide shows each command with real output, then covers the two problems beginners hit most: a rejected push and a merge conflict.

Version Control Basics: Commit, Push, and Pull

The three places your code lives

Commit, push and pull only make sense once you know where Git keeps your work. There are three locations:

  1. Working directory: the files you edit in your editor.
  2. Local repository: the hidden .git folder on your machine. It stores every commit you have made. It also has a staging area (the index), which holds the changes you have marked for the next commit.
  3. Remote repository: a copy hosted somewhere else, usually GitHub, GitLab or Bitbucket. By convention the default remote is called origin.

Each command moves changes between these places:

CommandMoves changes fromToNeeds network?
git addWorking directoryStaging areaNo
git commitStaging areaLocal repositoryNo
git pushLocal repositoryRemote repositoryYes
git pullRemote repositoryLocal repository and working directoryYes

If you want the bigger picture of why teams use version control at all, read what version control is and why it is used first.

git commit: save a snapshot locally

A commit is a snapshot of the staged files plus some metadata: author, date, a message and a pointer to the previous commit (its parent). Git identifies each commit by a SHA-1 hash; you normally see the first seven characters.

Committing is a two-step process. First you stage what you want, then you commit it:

$ git status --short
 M index.html
?? contact.js

$ git add index.html contact.js
$ git commit -m "Add contact form validation"
[main 3f2a1c9] Add contact form validation
 2 files changed, 41 insertions(+), 3 deletions(-)
 create mode 100644 contact.js

In the git status --short output, M means a tracked file was modified and ?? means a new, untracked file. Your hash and line counts will be different from the ones shown here.

Points that trip people up:

  • A commit exists only on your machine until you push it. Nobody else can see it.
  • git commit -a -m "..." stages modified tracked files automatically, but it does not add new untracked files such as contact.js above.
  • Commit one logical change at a time, and write the message in the imperative: "Fix crash when email is empty", not "fixed stuff". Clear messages make git log and code review far easier.

git push: publish your commits

git push sends the commits your remote branch does not have yet, then moves the remote branch pointer forward.

$ git push origin main
...
To github.com:your-user/demo-site.git
   9b1e2d4..3f2a1c9  main -> main

The line 9b1e2d4..3f2a1c9 main -> main means the remote main moved from commit 9b1e2d4 to your commit 3f2a1c9.

The first time you push a new branch, add -u (short for --set-upstream). Git then remembers which remote branch it tracks, so later you can type plain git push and git pull:

$ git switch -c feature/contact-form
$ git push -u origin feature/contact-form

Pushing to GitHub needs authentication. GitHub stopped accepting account passwords for Git operations over HTTPS in 2021, so use an SSH key or a personal access token. GitHub's own documentation walks through both.

Why a push gets rejected

If a teammate pushed to main after your last pull, the remote has commits you do not. Git refuses to overwrite them:

$ git push origin main
To github.com:your-user/demo-site.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:your-user/demo-site.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. ...

The fix is to pull first, then push again. Do not reach for git push --force on a shared branch: it replaces the remote history with yours and deletes your teammate's commits from the branch.

git pull: bring in other people's work

git pull is two commands in one:

  1. git fetch downloads new commits from the remote and updates origin/main, your local record of where the remote branch is.
  2. Git then integrates origin/main into your current branch, by a merge (the default) or a rebase if you ask for one.

If you have no local commits of your own, the pull is a simple fast-forward: your branch pointer just moves ahead. If both sides have new commits, the branches have diverged. Recent Git versions then ask you to choose a strategy once:

# keep a merge commit when branches diverge (the classic behaviour)
git config --global pull.rebase false

# or replay your local commits on top of the remote ones (linear history)
git config --global pull.rebase true

Both are valid. Many teams prefer rebase for a tidy history. A merge is easier to undo while you are learning. If you only want to look at incoming changes before integrating them, run git fetch and then git log main..origin/main.

Resolving a merge conflict

A conflict happens when you and a teammate changed the same lines of the same file. Git stops the pull and marks the file:

$ git pull origin main
Auto-merging config.js
CONFLICT (content): Merge conflict in config.js
Automatic merge failed; fix conflicts and then commit the result.

Open config.js and you will see both versions between markers:

<<<<<<< HEAD
const MAX_NAME_LENGTH = 50;
=======
const MAX_NAME_LENGTH = 80;
>>>>>>> 4c7d1e0...

The part above ======= is yours (HEAD). The part below is the incoming change, labelled with the commit it came from. Edit the file so it contains the code you actually want and delete all three marker lines. Then finish the merge:

$ git add config.js
$ git commit          # Git pre-fills a "Merge branch 'main' of ..." message
$ git push origin main

If you get lost halfway, git merge --abort puts everything back the way it was before the pull.

A complete daily workflow

Here is the loop most developers repeat many times a day on a feature branch:

git switch main
git pull                       # start from the latest code
git switch -c fix/signup-crash # one branch per task
# ...edit files...
git status                     # see what changed
git diff                       # review the exact lines
git add src/signup.js
git commit -m "Fix crash when signup email is empty"
git push -u origin fix/signup-crash
# open a pull request on GitHub, get it reviewed, merge it

Working on a branch and merging through a pull request keeps main stable and gives teammates a chance to review your change. For a longer walk-through of branches and pull requests, see how to use Git and GitHub in a real project.

Undoing things safely

SituationCommandSafe after pushing?
Staged a file by mistakegit restore --staged file.jsNot relevant (nothing committed)
Typo in the last commit message, or forgot a filegit commit --amendNo: it rewrites the commit
Undo the last commit but keep the changesgit reset --soft HEAD~1No: it rewrites history
Undo a commit that is already on the remotegit revert <hash>Yes: it adds a new commit that reverses the old one

The rule of thumb: rewrite history only for commits you have not pushed. For anything already shared, use git revert.

Practise it in ten minutes

  1. Create an empty repository on GitHub and clone it.
  2. Add a README.md, commit it and push it.
  3. Edit the README directly on GitHub's website and commit there. This plays the role of "a teammate".
  4. Change the same line locally, commit, and try to push. You will see the rejection.
  5. Pull, resolve the conflict, commit and push.

Doing this once makes the whole cycle concrete. After that, try a small project from these GitHub practice projects for full stack learners. If you are new to Git itself, what Git is and how to install and use it covers setup, and version control explained for full stack learners shows where Git fits in a web project.

FAQs

What is the difference between git commit and git push?

git commit saves a snapshot of your staged changes in your local repository only. git push uploads those local commits to a remote repository such as GitHub so other people can see them.

What is the difference between git fetch and git pull?

git fetch only downloads new commits and updates remote-tracking branches like origin/main; it does not touch your files. git pull runs a fetch and then merges or rebases those commits into your current branch.

Why should I pull before I push?

If the remote branch has commits you do not have, Git rejects your push. Pulling first brings those commits in and lets you resolve any conflicts locally before you push.

Can I undo a commit I have already pushed?

Yes. Use git revert with the commit hash. It creates a new commit that reverses the change, so the shared history is not rewritten and teammates are not affected.

Git, GitHub and branch-based teamwork are covered hands-on in Uncodemy's full stack developer course.

Placed Students

Our Clients

Partners

...