Backend Web Development

What Is Version Control? A Beginner’s Guide to Git

What Is Version Control? A Beginner’s Guide to Git
TutorialBeginner · about 30 minutes · hands-on

Have you ever saved a file as report_final_v2_REAL.docx? Git solves that problem for code, and for any text you care about. It remembers every version, lets you try ideas on a safe copy, and lets a team work on the same project without overwriting each other. In this tutorial you will install Git, make a repository, commit changes, branch, merge, fix a conflict and push your work to GitHub. There is also a practice terminal on this page that you can use right now, no install needed.

Quick facts
You will buildA small Git repository with history, a branch and a merge
You needA computer, a terminal, and a free GitHub account for the last step
LevelBeginner
TimeAbout 30 minutes
Reading time10 min read
95%of developers use Git as their main tool (a 2022 survey, as reported on Wikipedia)
2005year Linus Torvalds started Git
17M+developers in India on GitHub in 2024
In this guide

    Why version control exists

    Without version control, two people editing the same file send copies by email and then argue about whose version is the real one. A version control system keeps a timeline of snapshots, called commits. Each commit records what changed, who changed it and why. You can compare any two snapshots, go back to an old one, or work on a new idea without touching the working version.

    Git was created in April 2005 by Linus Torvalds after the free licence for BitKeeper, the tool that the Linux kernel developers used, was withdrawn. He wanted a fast, distributed system, where every developer has the whole history on their own computer. Junio Hamano took over maintenance in July 2005 and released version 1.0 in December 2005.

    Step 0: install Git and say who you are

    Download Git from git-scm.com for Windows or macOS, or install it with your package manager on Linux (for example apt install git). Check that it works, then tell Git your name and email. They are attached to every commit you make.

    git --version
    git config --global user.name "Your Name"
    git config --global user.email "you@example.com"
    git config --global init.defaultBranch main
    Check your work
    git –version should print a version number such as git version 2.43.0. If your terminal says command not found, close it and open a new one after installing.

    The one picture to remember: three areas

    How a change travels through Git

    1. Working directory

    The files you see and edit. Git notices changes here but does not save them yet.

    2. Staging area (the index)

    git add moves a change here. Think of it as a basket where you choose what goes into the next snapshot.

    3. Repository (history)

    git commit takes everything in the basket and stores it forever as a commit with a message.

    4. Remote (GitHub)

    git push copies your commits to a server so others can see them, and git pull brings theirs back.

    Step 1: your first repository

    Open a terminal, make a folder and run the commands below. This is a real session that I ran with Git 2.43. I shortened the commit IDs and the folder path so they are easy to read, and everything else is as Git printed it.

    $ git init -b main
    Initialized empty Git repository in /home/you/project/.git/
    $ echo "Hello Git" > hello.txt
    $ git status
    On branch main
    
    No commits yet
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    	hello.txt
    
    nothing added to commit but untracked files present (use "git add" to track)
    $ git add hello.txt
    $ git status --short
    A  hello.txt
    $ git commit -m "Add hello file"
    [main (root-commit) 3f2a1bc] Add hello file
     1 file changed, 1 insertion(+)
     create mode 100644 hello.txt

    Read the output slowly: the first status says the file is untracked, so Git sees it but is not recording it. After git add the letter A means added to the staging area. After git commit the file is safely stored in history.

    Change something and look at the difference

    $ echo "Second line" >> hello.txt
    $ git diff
    diff --git a/hello.txt b/hello.txt
    index 8d0e412..b1a9c3f 100644
    --- a/hello.txt
    +++ b/hello.txt
    @@ -1 +1,2 @@
     Hello Git
    +Second line
    $ git commit -am "Add a second line"
    [main 7c9d0e4] Add a second line
     1 file changed, 1 insertion(+)

    Lines starting with a plus sign were added and lines with a minus sign were removed. The -a in -am means stage all changes to files Git already tracks, which saves one step. New files still need git add.

    Try it now: the practice terminal

    This is a small simulator of Git that runs in your browser. Nothing you type here touches your computer. Follow the buttons in order, then try your own commands. Type help to see what is supported. It is a simplified model: it tracks commits and branches, not the contents of files.

    Interactive: Git practice terminal

    Step 2: branch and merge

    A branch is a movable label on a commit. Creating one is instant and cheap, so Git users make a branch for every new idea. You can experiment safely, and if the idea works you merge it back. Here is a real run, again with shortened IDs.

    $ git switch -c feature
    Switched to a new branch 'feature'
    $ echo "Feature work" > feature.txt && git add feature.txt && git commit -m "Add feature file"
    [feature a41b9d2] Add feature file
     1 file changed, 1 insertion(+)
     create mode 100644 feature.txt
    $ git switch main
    Switched to branch 'main'
    $ git merge feature
    Updating 7c9d0e4..a41b9d2
    Fast-forward
     feature.txt | 1 +
     1 file changed, 1 insertion(+)
     create mode 100644 feature.txt
    $ git log --oneline
    a41b9d2 (HEAD -> main, feature) Add feature file
    7c9d0e4 Add a second line
    3f2a1bc Add hello file

    The merge was a fast-forward: main had no new commits of its own, so Git simply moved the main label up to the feature commit. If both branches had changed, Git would create a new merge commit instead, which the practice terminal also shows.

    Step 3: fix a merge conflict

    Conflicts sound scary but are simple. They happen when two branches change the same line. Git cannot guess which one you want, so it stops and asks you. This is what happens when two branches rename the same title differently, from a real run.

    $ git merge feature
    Auto-merging page.txt
    CONFLICT (content): Merge conflict in page.txt
    Automatic merge failed; fix conflicts and then commit the result.
    $ cat page.txt
    <<<<<<< HEAD
    Title: My studio
    =======
    Title: My blog
    >>>>>>> feature
    $ git status --short
    UU page.txt
    How to resolve it

    1. Open the file

    Find the lines between the markers. Above the equals line is your branch. Below is the incoming branch.

    2. Choose the final text

    Keep one, keep both, or write something new. Delete the three marker lines: the angle brackets and the equals line.

    3. Mark it resolved

    Run git add page.txt to tell Git the conflict in that file is solved.

    4. Finish the merge

    Run git commit. Git offers a merge message, so save and close the editor. Done.

    Step 4: put it on GitHub

    Git works on your computer. GitHub is a website that hosts Git repositories so you can back them up, share them and collaborate. Other hosts such as GitLab and Bitbucket work in a similar way. The steps are the same everywhere.

    1. Create a free account on GitHub and click New repository. Give it a name. Do not tick the option to add a README, since your folder already has history.
    2. Copy the repository address that GitHub shows you.
    3. In your project folder, connect it and push: use the commands below.
    4. Refresh the page on GitHub and you will see your files and commit history.
    git remote add origin https://github.com/YOUR-NAME/YOUR-REPO.git
    git push -u origin main
    
    # later, to get other people's changes
    git pull
    Never commit secrets
    Passwords, API keys and tokens must never go into a repository, even a private one. Anything committed stays in history. Keep them in a file named .env, add .env to a file called .gitignore, and rotate (replace) any key that was ever committed by mistake. When GitHub asks you to sign in from the terminal, use the browser sign-in or a personal access token, and never type your account password into scripts.

    Habits that make you look like a pro

    • Commit small and often. One idea per commit is easy to understand and easy to undo.
    • Write messages for humans. Say what and why: “Fix broken contact form email” beats “stuff”.
    • Use .gitignore. List files that should never be tracked, such as .env, node_modules and build output.
    • Branch for every feature or fix. Keep main working at all times.
    • Pull before you push. It avoids most conflicts.
    # example .gitignore
    .env
    node_modules/
    *.log
    .DS_Store

    Cheat sheet

    I want to…Command
    Start a repositorygit init
    See what changedgit status, then git diff
    Stage and savegit add file, then git commit -m “message”
    See historygit log –oneline
    Make and switch to a branchgit switch -c name
    Combine a branchgit merge name
    Discard changes in a filegit restore file
    Undo a commit safely (keeps history)git revert COMMIT-ID
    Download a projectgit clone ADDRESS
    Share my commitsgit push

    Troubleshooting

    Updates were rejected because the remote contains work that you do not have

    Someone pushed first. Run git pull, fix any conflicts, then push again.

    I committed a password

    Treat it as leaked. Replace the password or key right away, then remove it from the code. Removing it from history is possible but takes special tools, so rotating the secret is the first step.

    I am in detached HEAD state

    You checked out an old commit. Run git switch main to return, or git switch -c new-name to keep your work on a new branch.

    Git says nothing to commit but I changed a file

    Check you saved the file, that you are in the right folder, and that the file is not listed in .gitignore.

    Test yourself

    Test yourself

    Mind map: Git at a glance

    Git
    • Git
      • Areas
        • Working directory
        • Staging
        • Repository
        • Remote
      • Daily commands
        • status
        • add
        • commit
        • log
      • Branching
        • switch -c
        • merge
        • conflicts
      • Sharing
        • clone
        • push
        • pull
        • pull request
      • Safety
        • .gitignore
        • No secrets
        • revert
    Tap a branch to open it.

    India and open source

    Git is one of the reasons Indian developers can work on global projects from anywhere. GitHub’s 2024 Octoverse report counted more than 17 million developers in India on GitHub, growing about 28 percent in a year, and projected that India will become the largest developer community in the world by 2028. A public GitHub profile with real projects is one of the best portfolios a student can build.

    Where to go next

    Sources

    Command output was produced by running Git 2.43 in a test folder. Commit IDs and the folder path were shortened for readability.

    1 Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *