Git
Beginners Guide
Programming
Source Control
Software Development

Git for beginners The definitive practical guide

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Git is a powerful and widely used version control system that allows multiple developers to work on the same project without interfering with each other's work. This tool is essential for coordinating team projects, as well as tracking and merging changes in any set of files, particularly source code.

What is Git?

Git is a distributed version control system created by Linus Torvalds in 2005. It’s designed to handle projects of all sizes with speed and efficiency. Unlike centralized version control systems, Git gives every developer their own local repository, complete with a full history of commits. This means that Git works offline, and when online, only changes are transferred, which makes operations fast.

Installing Git

To start using Git, you need to install it on your computer. Git is available for all major operating systems. You can download it from git-scm.com.

Linux:

bash
sudo apt-get install git

macOS:

bash
brew install git

Windows:

Download the executable file from the Git website and follow the installation instructions.

Basic Git Commands

Here are some basic commands to get you started with Git:

  • git init: Initializes a new Git repository. This command creates a new subdirectory named .git.
  • git clone [url]: Creates a copy of an existing repository from a remote location.
  • git add [file]: Adds a file to the staging area, preparing it for inclusion in the next commit.
  • git commit -m "[message]": Records your changes to the repository with a descriptive message.
  • git status: Displays the state of the working directory and staging area.
  • git log: Shows the commit history.

Working with Branches

Branches are a core concept in Git that allow you to diverge from the main line of development and continue to work on changes without affecting the main line.

Creating a Branch:

bash
git branch [branch_name]

Switching Branches:

bash
git checkout [branch_name]

Merging Branches:

Once you've completed work on a branch, you may need to bring that work into another branch (usually the main or master branch).

bash
git checkout main
git merge [branch_name]

Pushing Changes to a Remote Repository

To share your changes with other developers, you’ll need to push your changes to a remote repository.

bash
git push [remote_name] [branch_name]

Where remote_name is usually origin, the default name Git gives to the server you cloned from.

Pulling Updates from Remote Repository

To update your local repository to the newest commit, execute:

bash
git pull [remote_name] [branch]

This command fetches and merges changes from the remote server to your working directory.

Handling Merge Conflicts

Conflicts arise when multiple changes are made to the same part of a file in different branches. Git will mark the file as conflicted and halt the merging process. You need to resolve the conflicts manually by editing the files shown by Git and then mark them as resolved by running:

bash
git add [resolved-file]

Key Points Summary

CommandDescriptionExample
git initInitialize a new Git repositorygit init
git cloneCopy a remote repositorygit clone https://...
git addAdd file(s) to staging areagit add .
git commitCommit changesgit commit -m "Add feature"
git statusList the status of filesgit status
git logShow commit logsgit log
git branchList, create, or delete branchesgit branch -a
git checkoutSwitch branches or restore filesgit checkout develop
git mergeMerge two branchesgit merge feature_branch
git pushSend changes to remote repositorygit push origin main
git pullUpdate local repository to remotegit pull origin main

Conclusion

Git is an indispensable tool for modern software development. Its powerful features like branching and merging allow teams to collaborate efficiently. This guide touches on the basic tools and commands needed to start working with Git. As you grow in your software development career, you’ll find Git to be an invaluable resource for managing your codebase.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.