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:

  1. Open the file(s) with conflicts.
  2. Edit the file(s) to resolve conflicts manually.
  3. Save the changes.
  4. Stage the resolved files using git add.
  5. Commit the changes.

Top 20 Git Commands Cheat Sheet

  1. git init: Initialize a new Git repository.
  2. git clone <repository-url>: Clone an existing Git repository.
  3. git branch <branch-name>: Create a new branch.
  4. git checkout <branch-name>: Switch to a different branch.
  5. git checkout -b <branch-name>: Create and switch to a new branch.
  6. git add <file-name>: Stage changes for commit.
  7. git commit -m "Commit message": Commit staged changes.
  8. git tag <tag-name> <commit-hash>: Create a new tag.
  9. git reset <file-name>: Unstage changes for a specific file.
  10. git reset: Unstage all changes.
  11. git reset --hard <commit-hash>: Reset to a previous commit.
  12. git push <remote-name> <branch-name>: Push changes to a remote repository.
  13. git pull <remote-name> <branch-name>: Pull changes from a remote repository.
  14. git merge <branch-name>: Merge changes from a different branch.
  15. git rebase <branch-name>: Reapply commits on top of another branch.
  16. git log: View commit history.
  17. git status: Check the status of your working directory.
  18. git diff: View the differences between files.
  19. git remote add <remote-name> <repository-url>: Add a remote repository.
  20. 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.