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.
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:
macOS:
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:
Switching Branches:
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).
Pushing Changes to a Remote Repository
To share your changes with other developers, you’ll need to push your changes to a remote repository.
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:
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:
Key Points Summary
| Command | Description | Example |
git init | Initialize a new Git repository | git init |
git clone | Copy a remote repository | git clone https://... |
git add | Add file(s) to staging area | git add . |
git commit | Commit changes | git commit -m "Add feature" |
git status | List the status of files | git status |
git log | Show commit logs | git log |
git branch | List, create, or delete branches | git branch -a |
git checkout | Switch branches or restore files | git checkout develop |
git merge | Merge two branches | git merge feature_branch |
git push | Send changes to remote repository | git push origin main |
git pull | Update local repository to remote | git 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
- Git for beginners The definitive practical guide
- Git for Windows .bashrc or equivalent configuration files for Git Bash shell
- git grep by file extensions
- git hooks is there a clone hook?
- Git, How do I list only local branches?
- Git How do I list only local branches?
- git how to disable push
- git how to move some commits to new branch
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.