Git Basics for Beginners

Git Basics for Beginners
Photo by Farhat Altaf / Unsplash

Git is a powerful and widely used version control system that enables developers to track changes in source code and collaborate efficiently. This beginner-friendly guide provides an essential overview of Git's fundamental concepts and commands.


What is Git?

Git is a distributed version control system used to manage and track changes in code. It allows multiple people to work on the same project simultaneously without overwriting each other's work.


Installing Git

To install Git on Ubuntu:

sudo apt update
sudo apt install git

To check the installation:

git --version

Initial Configuration

Before using Git, configure your identity:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Common Git Terminology

  • Repository: A directory containing your project and the .git folder.
  • Commit: A snapshot of changes in the repository.
  • Branch: A parallel version of the repository.
  • Merge: Combining different branches.
  • Clone: A copy of a remote repository.

Basic Git Workflow

  1. Create or Clone a Repository

Pull Changes from Remote

git pull origin main

Push Changes to Remote

git push origin main

Merge Branches

git checkout main
git merge new-feature

Create and Switch Branches

git branch new-feature
git checkout new-feature

View Commit History

git log

Commit Changes

git commit -m "Describe your changes"

Track Files

git add <filename>  # Add a single file
git add .            # Add all changes

Check Status

git status

Clone an existing repository:

git clone <repository-url>

Create a new repository:

git init

.gitignore File

Use .gitignore to exclude files and directories from being tracked:

*.log
venv/
__pycache__/
.env

Useful Tips

  • Commit often with meaningful messages.
  • Regularly pull from remote to stay up to date.
  • Use branches for features or bug fixes.
  • Keep your main branch clean and production-ready.

Conclusion

Git is essential for modern software development. Mastering its basics lays the foundation for efficient collaboration and code management. As you grow comfortable with the commands above, consider exploring more advanced Git concepts such as rebasing, stashing, and resolving merge conflicts.