Skip to content
Tech

Git and GitHub for Beginners: Commands, Branches and Merge Conflicts

Learn Git and GitHub step by step: setup, init, add, commit, push, pull, branches and merging, resolving a real merge conflict, .gitignore and a handy cheat-sheet.

Aerial view of a river splitting into two channels around a green island and rejoining downstream, with forested banks in morning mist
Aerial view of a river splitting into two channels around a green island and rejoining downstream, with forested banks in morning mist
In this article

    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 --version

    Then 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 main

    The 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 app

    The 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 main

    origin 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.git

    Step 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/greeting
    Switched 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 push

    git 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/title
    Auto-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/title

    Everything 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:

    1. 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.
    2. Stage the fixed file: git add README.md
    3. 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 app

    Changed 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 pull
    Updating 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_Store

    Check 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

    SituationCommand
    Unstage a file you added by mistakegit restore --staged FILE
    Throw away uncommitted edits to a filegit 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 safelygit revert COMMIT_ID (creates a new commit that reverses it)

    Git command cheat-sheet

    CommandWhat it does
    git config --global user.name "Name"Set your name for commits
    git initStart a new repository in the current folder
    git clone URLCopy a remote repository to your computer
    git statusShow 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 --graphShow compact history
    git diffShow unstaged changes
    git remote add origin URLConnect a remote repository
    git push -u origin mainFirst push of a branch; sets tracking
    git push / git pullSend / receive commits
    git branchList branches
    git switch -c NAMECreate and switch to a new branch
    git switch NAMESwitch to an existing branch
    git merge NAMEMerge a branch into the current one
    git merge --abortCancel a merge with conflicts
    git branch -d NAMEDelete a merged branch
    git restore FILEDiscard local edits to a file
    git revert COMMIT_IDReverse 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.

    Sources

    Written by Raj

    Founder & Editor

    I write HorizonDesk: practical, no-hype articles on technology, money, business and the vehicles we drive. Everything here is something I’ve researched, tested or wanted explained clearly myself.