Git is a distributed version control system that helps developers track changes, collaborate with others, and manage different versions of a project. This tutorial introduces the basic Git commands and workflows.
## 1. What Is Git?
Git allows you to:
- Track changes in files
- Restore previous versions
- Create and manage branches
- Collaborate with other developers
- Work offline
- Merge changes from different contributors
Git is commonly used together with online platforms such as GitHub, GitLab, and Bitbucket.
---
## 2. Installing Git
Download Git from the official website:
[https://git-scm.com/](https://git-scm.com/)
After installation, check whether Git is available:
```bash
git --version
```
You should see a result similar to:
```bash
git version 2.43.0
```
---
## 3. Configuring Git
Before using Git, set your username and email address:
```bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
```
You can view your configuration with:
```bash
git config --list
```
These details are used to identify who made each commit.
---
## 4. Creating a Git Repository
Create a new project directory:
```bash
mkdir my-project
cd my-project
```
Initialize Git:
```bash
git init
```
This command creates a hidden `.git` directory that stores the repository information.
---
## 5. Checking File Status
To see which files have been modified or added, use:
```bash
git status
```
For example, Git may show:
```bash
Untracked files:
index.html
```
This means that `index.html` exists in the project directory but has not yet been added to Git.
---
## 6. Adding Files to the Staging Area
Add a specific file:
```bash
git add index.html
```
Add all changed files:
```bash
git add .
```
The staging area allows you to choose which changes will be included in the next commit.
---
## 7. Creating a Commit
A commit saves the staged changes to the local repository:
```bash
git commit -m "Add homepage"
```
A good commit message should clearly describe the changes.
Examples:
```bash
git commit -m "Fix login validation"
git commit -m "Update documentation"
git commit -m "Add user profile page"
```
---
## 8. Viewing Commit History
Display the complete commit history:
```bash
git log
```
Use a shorter format:
```bash
git log --oneline
```
Example:
```bash
a1b2c3d Add homepage
e4f5g6h Fix navigation bug
```
---
## 9. Viewing Changes
To see changes that have not been staged:
```bash
git diff
```
To see changes that have already been staged:
```bash
git diff --staged
```
These commands are useful for reviewing your work before committing.
---
## 10. Working with Branches
A branch allows you to develop features without changing the main code directly.
Create a new branch:
```bash
git branch feature-login
```
Switch to the branch:
```bash
git switch feature-login
```
You can also create and switch to a branch in one command:
```bash
git switch -c feature-login
```
List all branches:
```bash
git branch
```
The current branch is marked with an asterisk.
---
## 11. Merging Branches
After finishing your work, switch to the main branch:
```bash
git switch main
```
Merge the feature branch:
```bash
git merge feature-login
```
If there are no conflicts, Git will merge the changes automatically.
After merging, you can delete the feature branch:
```bash
git branch -d feature-login
```
---
## 12. Handling Merge Conflicts
A merge conflict occurs when two branches modify the same part of a file.
Git may mark the conflict like this:
```text
<<<<<<< HEAD
Current branch content
=======
Incoming branch content
>>>>>>> feature-login
```
To resolve the conflict:
1. Open the conflicted file.
2. Choose the correct content.
3. Remove the conflict markers.
4. Add the resolved file:
```bash
git add filename
```
5. Complete the merge:
```bash
git commit -m "Resolve merge conflict"
```
---
## 13. Connecting to a Remote Repository
Create a repository on GitHub, then connect it to your local repository:
```bash
git remote add origin https://github.com/username/my-project.git
```
Check the remote repository:
```bash
git remote -v
```
Rename the current branch to `main` if necessary:
```bash
git branch -M main
```
---
## 14. Pushing Changes
Upload your local commits to the remote repository:
```bash
git push -u origin main
```
After the first push, you can usually use:
```bash
git push
```
The `-u` option sets the upstream branch, so future pushes require fewer arguments.
---
## 15. Cloning a Repository
To download an existing repository:
```bash
git clone https://github.com/username/project.git
```
Then enter the project directory:
```bash
cd project
```
Cloning downloads the project files and the complete Git history.
---
## 16. Pulling Changes
Download and merge changes from the remote repository:
```bash
git pull
```
This is important when working with a team because other developers may have uploaded new changes.
A typical workflow is:
```bash
git pull
git add .
git commit -m "Update project"
git push
```
---
## 17. Undoing Changes
### Discard changes in a file
```bash
git restore filename
```
### Remove a file from the staging area
```bash
git restore --staged filename
```
### Undo the latest commit but keep the changes
```bash
git reset --soft HEAD~1
```
### Undo the latest commit and remove the changes
```bash
git reset --hard HEAD~1
```
Be careful with `git reset --hard` because it may permanently delete uncommitted work.
---
## 18. Using a `.gitignore` File
A `.gitignore` file tells Git which files should not be tracked.
Example:
```gitignore
node_modules/
.env
*.log
dist/
.DS_Store
```
Create the file in the root directory of your project:
```bash
touch .gitignore
```
Common files to ignore include:
- Temporary files
- Passwords and environment variables
- Build directories
- Dependency folders
- Operating system files
---
## 19. Common Git Workflow
A basic daily workflow looks like this:
```bash
git pull
git switch -c new-feature
git status
git add .
git commit -m "Add new feature"
git switch main
git pull
git merge new-feature
git push
```
For a personal project, the workflow can be simpler:
```bash
git status
git add .
git commit -m "Update project"
git push
```
---
## 20. Useful Git Commands
| Command | Description |
|---|---|
| `git init` | Create a new repository |
| `git clone` | Download a repository |
| `git status` | Show the current status |
| `git add` | Add changes to staging |
| `git commit` | Save changes |
| `git log` | View commit history |
| `git diff` | View file differences |
| `git branch` | Manage branches |
| `git switch` | Change branches |
| `git merge` | Merge branches |
| `git pull` | Download remote changes |
| `git push` | Upload local changes |
| `git remote -v` | View remote repositories |
---
## Conclusion
Git is an essential tool for modern software development. By learning the basic commands—such as `git add`, `git commit`, `git branch`, `git pull`, and `git push—you can effectively manage project versions and collaborate with other developers.
The best way to learn Git is to practice regularly with a small project. Start by creating a repository, making commits, creating branches, and synchronizing your work with GitHub.