Git 101: Simple Guide to Mastering Version Control

Git 101: Simple Guide to Mastering Version Control

Thank you for choosing to learn Git with me. This course provides a solid foundation in version control, helping you manage your projects efficiently. If you find this course helpful, please leave a ⭐️ on my GitHub.

Understanding Git Commands: A Structured Approach

Git is an essential tool for version control, widely used by developers to manage code efficiently. To make the most of Git, it is crucial to understand its command structure and workflow. This blog post will guide you through Git commands in a structured manner.

1. Cloning a Repository

The git clone command is used to create a local copy of a remote repository. It downloads the entire repository, including all branches, history, and files.

Syntax:

git clone <repository-url>

Example:

git clone https://github.com/user/repository.git

2. Adding Changes to Staging

Before committing changes, they must be added to the staging area. This step ensures that only specific files are included in the next commit.

Syntax:

git add <filename>

To add all modified and new files:

git add .

3. Committing Changes

Once files are staged, commit them with a meaningful message.

Syntax:

git commit -m "Commit message"

To add and commit all changes in one step:

git commit -am "Commit message"

4. Pushing Changes to Remote Repository

After committing, push the changes to the remote repository.

Syntax:

git push origin master

To push to a different branch:

git push -u origin <branch-name>

5. Pulling Changes from Remote Repository

To update the local repository with the latest changes from the remote repository:

Syntax:

git pull origin <branch-name>

This fetches the latest changes and merges them into your local branch.

6. Working with Branches

Branches help manage different versions of a project and facilitate parallel development.

Checking Available Branches

git branch

Switching to a Different Branch

git checkout <branch-name>

Creating a New Branch and Switching to It

git checkout -b <new-branch-name>

Deleting a Branch

git branch -d <branch-name>

7. Checking Status of Files

To check which files have been modified, added, or deleted:

git status

8. Managing Remote Repositories

Viewing Remote Repository URLs

git remote -v

Adding a Remote Repository

git remote add origin <repository-url>

9. Viewing Commit History and Differences

Viewing Commit History with Merge Details

git log --merge

Checking Differences Between Commits

git diff

10. Resetting Changes

Resetting allows undoing changes made to files or commits.

Reset to a Previous Commit Without Deleting Changes

git reset --mixed <commit-hash>

Reset and Remove All Changes After a Certain Commit

git reset --hard <commit-hash>

Undo the Last Commit but Keep Changes Staged

git reset --soft HEAD~1

11. Merging Branches

To merge another branch into the current branch:

git merge <branch-name>

To abort a merge in case of conflicts:

git merge --abort

12. Writing Good Commit Messages

A good commit message should be clear and concise:

  • Use the imperative mood (e.g., "Fix bug" instead of "Fixed bug").
  • Keep the subject line under 50 characters.
  • Provide a detailed explanation if necessary.

13. Creating and Managing Pull Requests

A pull request (PR) allows contributors to propose changes to a repository. Steps to create a PR:

  1. Push changes to a feature branch.
  2. Navigate to GitHub/GitLab/Bitbucket and create a PR.
  3. Provide a descriptive title and explanation.
  4. Request reviews from team members.
  5. Address feedback and make necessary changes.
  6. Merge the PR once approved.

14. Handling Issues and Merge Conflicts

Tracking Issues

GitHub and GitLab allow tracking issues with descriptions and labels to organize work efficiently.

Fixing Merge Conflicts

  • Identify conflicting files using git status.
  • Manually resolve conflicts by editing the files.
  • Stage the resolved files with git add <filename>.
  • Commit the merge using git commit.
  • Push the resolved changes to the remote repository.

By mastering these Git commands and best practices, developers can efficiently manage their code and collaborate seamlessly. Happy coding!