In this article, we will explore the fundamentals of Git, a widely-used version control system in software development. You will learn what version control is, why it is essential, and how to use Git effectively through a step-by-step guide. By the end of this article, you’ll be comfortable with the basic commands and workflows necessary to manage your projects using Git.

Step 1: Installation of Git

To begin using Git, you need to have it installed on your system. Follow these instructions to install Git:

  1. Windows: Download the Windows installer from the Git website and run the installer.
  2. macOS: You can install Git using Homebrew. Open the Terminal and run the command: brew install git
  3. Linux: Use the package manager for your distribution. For example, on Ubuntu, run: sudo apt-get install git

Step 2: Configuring Git

After installing Git, you should configure it to set your identity. This can be done with the following commands:

  1. Open your command line interface.
  2. Set your username: git config --global user.name 'Your Name'
  3. Set your email: git config --global user.email 'your.email@example.com'
  4. You can verify your configuration by running git config --list.

Step 3: Creating a New Repository

In this step, we’ll create a new repository. Follow these steps:

  1. Navigate to the folder where you want your project to reside using the command line.
  2. Run the command git init to create a new Git repository.
  3. Add files to your repository directory.
  4. Track these files with the command: git add . (the dot means 'add everything').
  5. Now check the status of your repository by running git status.

Step 4: Committing Changes

To save your changes in the version history, you need to commit. Here's how:

  1. After adding your files, run git commit -m 'Your commit message'. The message should describe what changes were made.
  2. You can commit as many times as necessary to document your progress.

Step 5: Checking the Commit History

To view the history of your commits, you can use the following command:

  1. git log - This will show you a list of all commits along with their unique ID hashes.

Step 6: Working with Branches

Branches allow you to work on different features or fixes without affecting the main codebase. Here’s how to create and switch branches:

  1. Create a new branch: git branch new-branch-name.
  2. Switch to your new branch: git checkout new-branch-name.
  3. To combine your changes back into the main branch, switch back to the main branch and run git merge new-branch-name.

Step 7: Collaborating with Others

If you’re collaborating with others, you'll need to understand how to push changes and pull updates:

  1. Push your changes to a remote repository using: git push origin branch-name.
  2. Pull updates from a remote repository using: git pull origin branch-name.

Step 8: Resolving Merge Conflicts

When multiple people are working on the same file, conflicts can arise. Here’s how to resolve them:

  1. When a conflict occurs, Git will mark the files that need resolution.
  2. Edit the file(s) to resolve the conflicts manually.
  3. After resolving conflicts, add the changes: git add resolved-file.
  4. Then commit the resolved changes: git commit -m 'Resolved merge conflict'.

Summary

Today, we covered the basics of Git, from installation to collaboration. You learned how to:

  • Install and configure Git.
  • Create and manage a repository.
  • Commit changes and check history.
  • Work with branches and collaborate with others.
  • Resolve merge conflicts.

Remember that mastering Git takes practice, so don't hesitate to explore further and leverage resources like the official Git documentation for deeper understanding. Happy coding!