Git is a version control system that helps developers track changes in their code and collaborate with others. This guide introduces common Git commands and workflows for beginners.
Initializes a new Git repository in your project folder. Example: git init
Copies an existing Git repository from a remote server (like GitHub) to your local machine. Example: git clone https://github.com/user/repo.git
Stages changes to be committed. You can add specific files or all changes. Examples: git add filename.txt git add .
Saves the staged changes to the repository with a message describing the changes. Example: git commit -m "Add new feature"
Uploads your local commits to the remote repository. Example: git push origin main
Downloads changes from the remote repository and integrates them into your local branch. Example: git pull origin main
Reapplies your local commits on top of the latest changes from the remote branch. This keeps history clean. Example: git pull --rebase origin main
If you see an error like: ! [rejected] main -> main (fetch first) It means the remote branch has changes you don't have locally. To fix this: git pull --rebase origin main git push origin main
To make Git always rebase when pulling: git config --global pull.rebase true
While 'git pull --rebase' is useful for maintaining a clean and linear commit history, there are several scenarios where using 'git pull' with merge is a better choice:
- Collaborative Feature Branches: When multiple contributors are working on the same branch, rebasing can rewrite history and cause confusion. Merging preserves everyone's commit history and avoids conflicts.
- Tracking History for Debugging: Merge commits help retain the exact sequence of events, which is useful for auditing and debugging complex workflows.
- CI/CD Pipelines or Release Branches: Merges clearly show integration points, which are important for deployment and release tracking.
- Conflict-Prone Histories: If frequent conflicts are expected, merging may be easier to manage than rebasing, which applies commits one by one.
- You're Not Comfortable with Rebase Yet: Beginners may find merge easier to understand and safer to use until they are confident with resolving rebase conflicts.