A Comprehensive Git Tutorial
Introduction to Git
Git is a distributed version control system (DVCS) that helps developers track changes in their code and collaborate on projects efficiently. It's essential for managing source code and ensuring version control. In this tutorial, we will cover the basics of Git, including branching, committing, tagging, resetting, pushing, pulling, and resolving conflicts.
Basic Git Commands
Initializing a Repository
To create a new Git repository:
git init
Cloning a Repository
To clone an existing repository:
git clone <repository-url>
Basic Branching
To create a new branch:
git branch <branch-name>
To switch to a different branch:
git checkout <branch-name>
Shortcut to create and switch to a new branch:
git checkout -b <branch-name>
Committing Changes
To stage and commit changes:
git add <file-name>
git commit -m "Commit message"
Tagging
To create a new tag for a specific commit:
git tag <tag-name> <commit-hash>
Resetting Changes
To unstage changes:
git reset <file-name>
To unstage all changes:
git reset
To reset to a previous commit:
git reset --hard <commit-hash>
Pushing and Pulling
To push changes to a remote repository:
git push <remote-name> <branch-name>
To pull changes from a remote repository:
git pull <remote-name> <branch-name>
Resolving Conflicts
When conflicts occur during a merge or pull, Git will mark the conflicting lines in your files. To resolve conflicts:
- Open the file(s) with conflicts.
- Edit the file(s) to resolve conflicts manually.
- Save the changes.
- Stage the resolved files using
git add
. - Commit the changes.
Top 20 Git Commands Cheat Sheet
git init
: Initialize a new Git repository.git clone <repository-url>
: Clone an existing Git repository.git branch <branch-name>
: Create a new branch.git checkout <branch-name>
: Switch to a different branch.git checkout -b <branch-name>
: Create and switch to a new branch.git add <file-name>
: Stage changes for commit.git commit -m "Commit message"
: Commit staged changes.git tag <tag-name> <commit-hash>
: Create a new tag.git reset <file-name>
: Unstage changes for a specific file.git reset
: Unstage all changes.git reset --hard <commit-hash>
: Reset to a previous commit.git push <remote-name> <branch-name>
: Push changes to a remote repository.git pull <remote-name> <branch-name>
: Pull changes from a remote repository.git merge <branch-name>
: Merge changes from a different branch.git rebase <branch-name>
: Reapply commits on top of another branch.git log
: View commit history.git status
: Check the status of your working directory.git diff
: View the differences between files.git remote add <remote-name> <repository-url>
: Add a remote repository.git fetch <remote-name>
: Fetch changes from a remote repository.
Conclusion
Git is a powerful version control system that is essential for collaborative software development and project management. By understanding these basic Git commands, you can efficiently manage your code, collaborate with others, and track changes in your projects. Practice and explore Git further to become a proficient developer.