What Is Git and Why Does It Matter?
Git is a distributed version control system created in 2005 by Linus Torvalds, the creator of the Linux kernel. It tracks every change to your code throughout the software development process, allows you to go back in time, and enables seamless collaboration with your teammates on the same project.
Developing software without version control is like writing a book without saving any drafts. When you make a mistake, there is no point to return to. Trying different ideas becomes risky, and working with multiple people becomes nearly impossible. Git provides elegant solutions to all of these problems.
Installing Git and Initial Configuration
Installing Git
To start using Git, you first need to install it on your computer. The installation steps vary depending on your operating system:
- Windows: Download the installer from git-scm.com and run it. You can proceed with the default settings.
- macOS: Run
brew install gitin Terminal or install it automatically through Xcode Command Line Tools. - Linux: Use
sudo apt install git(Debian/Ubuntu) orsudo dnf install git(Fedora) depending on your distribution.
Initial Configuration
After installing Git, you need to set up your identity. This information is recorded with every commit and shows who made the changes:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
This configuration is done only once and applies to all your projects. If you want to use different information for a specific project, simply remove the --global parameter.
Essential Git Commands
Creating a New Repository
To start working with Git, you need to create a repository. To turn an existing folder into a Git repository, run the following command inside that folder:
git init
This command creates a hidden .git directory in your folder. All version history is stored in this directory.
Tracking Changes
Git's fundamental workflow consists of three stages: the working directory, the staging area, and the repository. You use the following commands to move between these stages:
git status— Shows the status of your changesgit add file.txt— Adds a specific file to the staging areagit add .— Adds all changes to the staging areagit commit -m "Descriptive message"— Saves the staged changes
Examining History
You can use the git log command to examine your project's history. This command lists all commits in chronological order. For a more compact view, you can use git log --oneline.
git log --oneline --graph --all
This command displays all branches as a graph, allowing you to visually track your project's development process.
Branch Management
Branches are one of Git's most powerful features. They allow you to develop new features, fix bugs, or make experimental changes without breaking your main codebase.
Creating and Switching Branches
To create a new branch and switch to it, you can use these commands:
git branch new-feature
git checkout new-feature
You can combine these two commands into a single line:
git checkout -b new-feature
In modern Git versions, the git switch command is also available:
git switch -c new-feature
Merging Branches
When your work on a branch is complete, you need to merge the changes back into the main branch:
git checkout main
git merge new-feature
Conflicts may arise during the merge process. Git marks the conflicting files and asks you to resolve them manually. After resolving the conflicts, you simply commit the changes.
Branching Strategies
Commonly used branching strategies in professional projects include:
- Git Flow: A comprehensive strategy that uses main, develop, feature, release, and hotfix branches
- GitHub Flow: A simpler approach where feature branches are created from main and merged via pull requests
- Trunk-Based Development: All developers make small, frequent commits directly to the main branch
Remote Repository Management with GitHub
GitHub is a platform that allows you to host your Git repositories in the cloud and share them with your teammates. Having become the hub for open source projects, GitHub is actively used by millions of developers worldwide.
Creating a GitHub Account and Repository
After creating a GitHub account, you can create a new repository. Pay attention to these options when creating a repository:
- Public or Private: Determines whether your project is visible to everyone or only to invited collaborators
- README file: Adds an initial file that describes your project
- .gitignore: Specifies files that should not be tracked
- License: Defines the terms of use for your project
Connecting a Local Repository to GitHub
To connect your local Git repository to a remote repository on GitHub, use these commands:
git remote add origin https://github.com/username/project.git
git push -u origin main
After the initial push, the git push command alone will suffice. To pull changes from the remote repository to your local repository, use the git pull command.
Pull Requests and Code Review
Pull requests (PRs) are one of GitHub's most important features. They allow you to submit your changes for review by teammates before merging them into the main branch.
Creating a Pull Request
- Push your changes from the feature branch to GitHub
- Click the "New Pull Request" button on GitHub
- Select the source and target branches
- Write a title and description explaining your changes
- Assign reviewers to examine your code
The Code Review Process
A good code review process improves software quality and strengthens knowledge sharing within the team. When reviewing, pay attention to the following points:
- Code readability and clarity
- Potential bugs and security vulnerabilities
- Test coverage and test quality
- Compliance with project standards
- Performance implications
Best Practices for Team Collaboration
Commit Messages
Well-written commit messages make it easier to understand a project's history. Follow these rules for effective commit messages:
- Write a short, concise subject line (50 characters or fewer)
- Add detailed explanations in the body if needed
- Use the imperative mood: "Fix bug" rather than "Fixed bug"
- Explain why the change was made, not what was done
Conflict Management
Conflicts are inevitable in team collaboration. To minimize and effectively manage conflicts, apply these strategies:
- Frequently pull updates from the main branch
- Make small, focused commits
- Avoid working on the same file simultaneously
- Carefully review both sides of changes when resolving conflicts
The .gitignore File
Every project has files that should not be tracked. You can specify build outputs, dependency folders, and environment variables in the .gitignore file to tell Git to ignore them:
node_modules/
*.log
.env
bin/
obj/
.vs/
Advanced Git Features
Using Stash
When you want to temporarily save your uncommitted changes, you can use the git stash command. This feature is extremely useful when you need to make an urgent fix or switch branches:
git stash
git stash pop
Rebase and Squash
Rebase is used to reorganize a branch's history. When you want to keep the commit history clean and linear, you can prefer rebase over merge:
git rebase main
Squash allows you to combine multiple commits into a single commit. This is particularly useful for consolidating numerous small commits in feature branches into one meaningful commit.
Git Hooks
Git hooks are scripts that run automatically when specific Git events occur. They can be used for tasks such as pre-commit code formatting, running tests, or validating commit messages. The popular tool Husky simplifies Git hook management in JavaScript projects.
Automation with GitHub Actions
GitHub Actions is a powerful tool for automating CI/CD (Continuous Integration/Continuous Deployment) processes. You can run automated tests, perform code quality checks, and deploy your application on every push or pull request.
GitHub Actions workflows are defined in YAML files and stored in the .github/workflows directory. Ready-made actions can be obtained from the marketplace, or you can write your own custom actions.
Git and GitHub are indispensable tools in modern software development. Using these tools effectively increases your individual productivity and strengthens team collaboration. Start with small steps, learn the basic commands, and discover advanced features over time.