Welcome to our comprehensive Git tutorial for beginners and intermediates! Today, we're going to dive into the world of Git, a powerful tool for version control that will help you manage your code like a pro. 💡
Git is a distributed version control system that allows you to track changes in your code, collaborate with others, and manage multiple versions of your project. It's a must-have tool for any developer!
Before we dive into using Git, let's get it installed on your machine.
Installation: Visit the official Git website and download the version suitable for your operating system. Follow the installation instructions to set up Git on your machine.
Check Installation: Open a terminal or command prompt and type git --version to confirm that Git is installed correctly.
Now that Git is installed, let's create a new repository for our project.
Initialize Git: Navigate to your project directory and initialize Git with the command git init. This creates a hidden .git folder in your project directory, which stores all the Git data.
First Commit: To save the initial state of your project, you need to commit it. Use the command git add . to add all the files in your project to the Git staging area, and then git commit -m "Initial commit" to commit the changes with a descriptive message.
Now that we have a Git repository set up, let's explore some essential Git commands.
git status 📝This command shows the current state of your Git repository. It tells you which files have been modified, which have been staged, and which are untracked.
git add 📝The git add command stages your changes, preparing them for the next commit. You can add individual files or all files with git add ..
git commit 📝After staging your changes with git add, you can commit them using git commit -m "Your commit message". Always include a descriptive message to explain the changes you've made.
git push 💡Once you've committed your changes, you can push them to a remote repository (like GitHub) using git push origin master. This sends your local repository's master branch to the remote repository named 'origin'.
Git has a standard workflow that helps maintain a clean and organized repository. Here's a simplified version:
Create a new branch: Before making significant changes, create a new branch to avoid affecting the master branch. Use the command git checkout -b branch-name.
Make changes and commit: Make your changes, stage them, and commit them.
Push the branch: Once you're satisfied with your changes, push the branch to the remote repository using git push origin branch-name.
Merge or rebase: After the changes have been pushed, merge or rebase the branch into the master branch. This combines the changes from the new branch into the master branch.
What does `git init` do?
Git makes collaborating on projects a breeze! In the next lesson, we'll explore how to use Git to collaborate with others, handle conflicts, and manage multiple contributors. Stay tuned! 🎯
Let's practice what we've learned by creating a simple Git repository and making some changes.
practice-repo.cd practice-repo && git init.index.html and add some content.git add index.html && git commit -m "Initial commit".git checkout -b new-branch.index.html and stage the changes.git commit -m "Changes in new-branch".git push origin new-branch.Now, you have a basic understanding of Git and how to use it to manage your code. Happy coding! 💡