Git is the version-control tool almost every software team uses, and GitHub is the most popular place to host Git repositories online. This beginner's guide walks through the commands you will use every day, in the order you will first need them: setting up, making commits, pushing to GitHub, branching and merging, fixing a merge conflict, ignoring files, and a cheat-sheet to keep handy. Every command and every output shown here was run for real on Git 2.47 (your commit IDs will be different).
Versions and facts in this guide were checked against official documentation on 26 October 2026 (IST). Tools move fast, so confirm version numbers on the linked official pages before you install.
Git vs GitHub in one minute
- Git is a program on your computer. It records snapshots (commits) of your project so you can see history, undo mistakes and work on several ideas in parallel. It works fully offline.
- GitHub is a website and service that hosts Git repositories, so you can back up your code, share it and collaborate through pull requests. GitLab and Bitbucket are alternatives that use the same Git commands.
Step 1: Install and configure Git
Download Git from git-scm.com (its downloads page listed 2.56.0 as the latest release on 26 October 2026), or use your package manager. On macOS, git is also available through the Xcode command line tools. On most Linux systems, sudo apt install git or sudo dnf install git works. Check the install:
git --versionThen tell Git who you are. This name and email are stored in every commit you make:
git config --global user.name "Asha Rao"
git config --global user.email "[email protected]"
git config --global init.defaultBranch mainThe last line makes new repositories start on a branch called main, which matches GitHub's default.
Step 2: Create a repository and make your first commit
mkdir my-project && cd my-project
git init
echo "# My Project" > README.md
printf 'print("Hello, Git!")\n' > app.py
git status --short?? README.md
?? app.py?? means "untracked": Git sees the files but is not recording them yet. Saving a change takes two steps. First stage it with git add, then commit it:
git add README.md app.py
git commit -m "Add README and app"
git log --oneline[main (root-commit) 7c1fa60] Add README and app
2 files changed, 2 insertions(+)
create mode 100644 README.md
create mode 100644 app.py
7c1fa60 Add README and appThe staging area lets you choose exactly what goes into each commit. git add . stages everything in the current folder, and git diff shows changes you have not staged yet.
Step 3: Push to GitHub
On GitHub, create a new empty repository (no README, so it does not clash with yours). GitHub shows you its URL. Connect it and push:
git remote add origin https://github.com/YOUR-USERNAME/my-project.git
git push -u origin mainorigin is just the conventional nickname for your main remote. -u links your local main to the remote one, so from then on a plain git push or git pull is enough. The output looks like this:
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.Authentication: GitHub no longer accepts your account password for Git operations over HTTPS. Its documentation lists the options: use a personal access token when Git asks for a password, let a credential helper such as Git Credential Manager handle sign-in, use the GitHub CLI, or set up SSH keys and use the SSH URL ([email protected]:YOUR-USERNAME/my-project.git).
Cloning an existing repository
To get a copy of a repository that is already on GitHub, including all its history:
git clone https://github.com/YOUR-USERNAME/my-project.gitStep 4: Branches and merging
A branch is a separate line of work. Create one for each feature or fix so main always stays working:
git switch -c feature/greeting
# edit app.py, then:
git add app.py
git commit -m "Add welcome message"
git switch main
git merge feature/greetingSwitched to a new branch 'feature/greeting'
[feature/greeting 2b253f4] Add welcome message
1 file changed, 1 insertion(+)
Switched to branch 'main'
Updating 7c1fa60..2b253f4
Fast-forward
app.py | 1 +
1 file changed, 1 insertion(+)"Fast-forward" means main had not changed since the branch was created, so Git simply moved main forward. Clean up and publish:
git branch -d feature/greeting
git pushgit switch is the modern command for changing branches. Older tutorials use git checkout -b, which still works. On GitHub, teams usually push the branch and open a pull request rather than merging locally, so someone can review the change first. Our guide to a simple Git branching workflow for small teams covers that process.
Step 5: Resolving a merge conflict
A conflict happens when two branches change the same lines differently. To show one, we changed the README title on a branch and changed it differently on main:
git switch -c feature/title
# README.md now says: # My Awesome Project
git commit -am "Change title on branch"
git switch main
# README.md now says: # My Python Project
git commit -am "Change title on main"
git merge feature/titleAuto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.Git stops and writes both versions into the file between conflict markers:
<<<<<<< HEAD
# My Python Project
=======
# My Awesome Project
>>>>>>> feature/titleEverything between <<<<<<< HEAD and ======= is your current branch. Everything from there to >>>>>>> is the incoming branch. git status --short marks the file UU (unmerged). To resolve it:
- Open the file and edit it to the final content you want, deleting all three marker lines. We kept a combination:
# My Awesome Python Project. - Stage the fixed file:
git add README.md - Finish the merge:
git commit -m "Merge feature/title, combine titles"
The history now shows both lines of work joining:
git log --oneline --graph* 96b9301 Merge feature/title, combine titles
|\
| * a6f6b17 Change title on branch
* | d4b915b Change title on main
|/
* 2b253f4 Add welcome message
* 7c1fa60 Add README and appChanged your mind halfway through? git merge --abort returns you to the state before the merge. Editors such as VS Code also highlight conflicts with buttons to accept either side.
Step 6: Pulling a teammate's work
When someone else pushes commits, bring them into your copy with git pull (which fetches, then merges):
git pullUpdating 96b9301..ec88610
Fast-forward
README.md | 2 ++
1 file changed, 2 insertions(+)A good habit: pull before you start work and before you push.
Step 7: Ignore files with .gitignore
Some files should never be committed: dependencies you can reinstall, build output, editor clutter and, above all, secrets such as .env files with passwords or API keys. List them in a .gitignore file at the root of the repository:
# Python
__pycache__/
*.pyc
.venv/
# Node
node_modules/
# Secrets and local settings
.env
# OS files
.DS_StoreCheck that a file is really ignored, and which rule matches it:
git check-ignore -v .env node_modules __pycache__.gitignore:10:.env .env
.gitignore:7:node_modules/ node_modules
.gitignore:2:__pycache__/ __pycache__Commit the .gitignore itself so everyone shares it. Note that it only affects untracked files. If you already committed a file by mistake, untrack it with git rm --cached FILE. If it contained a secret, treat that secret as leaked and rotate it, because it is still in history. GitHub also maintains a collection of starter .gitignore templates per language.
Undoing common mistakes
| Situation | Command |
|---|---|
| Unstage a file you added by mistake | git restore --staged FILE |
| Throw away uncommitted edits to a file | git restore FILE (cannot be undone) |
| Fix the message of your last commit (not yet pushed) | git commit --amend -m "New message" |
| Undo a pushed commit safely | git revert COMMIT_ID (creates a new commit that reverses it) |
Git command cheat-sheet
| Command | What it does |
|---|---|
git config --global user.name "Name" | Set your name for commits |
git init | Start a new repository in the current folder |
git clone URL | Copy a remote repository to your computer |
git status | Show changed, staged and untracked files |
git add FILE / git add . | Stage changes for the next commit |
git commit -m "message" | Save staged changes as a commit |
git log --oneline --graph | Show compact history |
git diff | Show unstaged changes |
git remote add origin URL | Connect a remote repository |
git push -u origin main | First push of a branch; sets tracking |
git push / git pull | Send / receive commits |
git branch | List branches |
git switch -c NAME | Create and switch to a new branch |
git switch NAME | Switch to an existing branch |
git merge NAME | Merge a branch into the current one |
git merge --abort | Cancel a merge with conflicts |
git branch -d NAME | Delete a merged branch |
git restore FILE | Discard local edits to a file |
git revert COMMIT_ID | Reverse a commit with a new commit |
Where to go next
Practise by putting every project under Git from day one. If you are still choosing a first language, see Python vs JavaScript vs Java. Git pairs naturally with the other guides in this set: version your REST API code, your Dockerfile and compose.yaml and your SQL scripts alongside the application.
FAQ
Do I need GitHub to use Git?
No. Git works entirely on your own computer. GitHub (or GitLab, Bitbucket) adds online backup, sharing and collaboration.
What is the difference between git pull and git fetch?
git fetch downloads new commits from the remote but does not change your files. git pull runs a fetch and then merges (or rebases, if configured) those commits into your current branch.
Why was my GitHub password rejected?
GitHub removed password authentication for Git operations. Use a personal access token, Git Credential Manager, the GitHub CLI or SSH keys instead.
master or main?
Both are just branch names. GitHub uses main by default for new repositories, and the init.defaultBranch setting makes local repositories match.