Now that we've introduced GitHub as your remote collaboration hub, let's dive into the fundamental language that makes it all possible: Git commands. Think of Git as the engine under the hood, and these commands are the steering wheel, accelerator, and brakes that let you navigate your project's history. We'll cover the most essential commands you'll use daily.
The git init command is your starting point for any new project that you want to track with Git. It initializes an empty Git repository in your current directory, creating a hidden .git folder where all the magic happens – storing your project's history and metadata.
git initThe git clone command is your gateway to existing projects. Whether it's a project on GitHub or another Git hosting service, this command downloads a complete copy of the repository, including its entire history, to your local machine. You'll typically use this when you want to start working on a project that's already been set up.
git clone <repository_url>After making changes to your files, you need to tell Git which of those changes you want to include in your next snapshot. git add stages these changes. It doesn't save them permanently yet, but it marks them for inclusion in the next commit. You can add specific files or all changes at once.
git add <file_name>
git add .Once your changes are staged with git add, you can permanently record them with git commit. Each commit is a snapshot of your project at a specific point in time. It's crucial to write clear and concise commit messages that explain what changes were made. This is the core of tracking your project's evolution.
git commit -m "Your descriptive commit message here"To see the history of your commits, the git log command is your best friend. It displays a list of all commits, showing their unique hash, author, date, and commit message. This is invaluable for understanding how your project has progressed and for pinpointing when certain changes were introduced.
git logWhile git log shows you the history, git status gives you an overview of your current working directory. It tells you which files have been modified, which are staged for commit, and which are untracked by Git. It's like a quick health check for your repository.
git statusWhen you're ready to share your local commits with others or to update your local repository with changes from a remote one, git push and git pull come into play. git push sends your committed changes from your local repository to a remote repository (like GitHub). git pull fetches and integrates changes from a remote repository into your local one.
git push origin <branch_name>
git pull origin <branch_name>Branches are a fundamental concept in Git that allow you to diverge from the main line of development. They're like parallel universes for your code, enabling you to work on new features or bug fixes without affecting the stable version. git branch lets you list, create, and delete branches.
git branch
git branch <new_branch_name>
git branch -d <branch_to_delete>Once you've created a branch and made some changes, you'll want to switch between them to work on different tasks. git checkout is the command you'll use for this. It allows you to navigate your project's history and switch between different branches.
git checkout <branch_name>When you're happy with the changes on a branch, you'll often want to merge them back into your main development line. git merge integrates changes from one branch into another. This is a key operation for bringing new features or fixes into your main project.
git merge <branch_to_merge>These commands form the bedrock of your Git experience. As you become more comfortable, you'll encounter more advanced commands, but mastering these basics will allow you to effectively manage your code and collaborate with others on GitHub.