photo
Published on

The Ultimate Git Workflow for Open Source Contributions.

Authors
  • avatar
    Name
    Jashandeep Singh
    Twitter

Introduction

Git stands as the predominant version control system in contemporary software development. A Git workflow serves as a set of guidelines or suggestions on how to employ Git systematically and efficiently to carry out tasks in a consistent and productive fashion.

Git is a distributed version control system, meaning that each user has a complete copy of the entire repository, including its history. This decentralization enables developers to work offline and collaborate seamlessly.

Table of Contents

Basic Concepts:

Repository:

A Git repository is essentially a storage space for your project. It contains all the files, history, and configuration information needed for your project.

Commit:

A commit is a snapshot of changes made to the repository. It includes a unique identifier (hash), the author's information, a timestamp, and a reference to the previous commit, forming a linked history of changes.

Branch:

A branch is a parallel version of the repository. It allows developers to work on separate features or fixes without affecting the main codebase. Git's branching system is lightweight, making it easy to create, switch, and merge branches.

Basic Commands:

git init:

Initialize a new Git repository.

git init

git clone:

Clone a repository from a remote source (e.g., GitHub).

git clone <repository_url>

git add:

Stage changes for the next commit.

git add <file>

git commit:

Create a new commit with the staged changes.

git commit -m "Commit message"

git log:

Display the commit history of a repository.

git log

git status:

get some information about the current working tree and pending changes.

get some information about the current working tree and pending changes.

git pull:

Fetch changes from a remote repository and integrate them into the current branch.

git pull origin <branch>

git push:

Upload local changes to a remote repository.

git push origin <branch>

git fetch:

This command is used to retrieve changes from a remote repository. It fetches any new branches or updates to existing branches from the remote, allowing you to see what changes others have made without actually merging those changes into your own work.

git fetch [remote-name]
  • [remote-name]: Specifies the remote repository to fetch changes from. If you don't provide a remote name, Git uses the default remote, usually named 'origin'.
git fetch

This command will fetch changes from the default remote ('origin').

Why Use 'git fetch'?

  1. Inspection before merging: allows you to inspect changes from a remote before merging them into your local branches. This is particularly useful to review changes and avoid unexpected conflicts.
  2. Efficient collaboration: Fetching updates from a remote helps keep your local repository in sync with the latest changes made by other contributors without immediately merging them.
  3. Safety: Fetching allows you to see changes without automatically merging them. This gives you the opportunity to decide when and how to integrate new changes into your local branches.

After fetching changes, you might decide to merge them into your local branch using git merge or git rebase. Alternatively, you can use git pull, which is a combination of git fetch and git merge (or git rebase if configured).

Remember to use git fetch regularly to keep your local repository up to date with the latest changes from the remote repository.

The term upstream in Git typically refers to the original or authoritative repository from which a clone was made. The upstream repository is the remote repository that serves as the source of truth for the project. When working with Git and GitHub, developers often use the term upstream to describe the primary repository to which they may send their changes.

Git Branches and Merging

git branch:

List, create, or delete branches.

git branch

Create a new branch in your repository.

git branch <name>

Delete a branch from your repository.

git branch -d <name>

Forces a branch to be deleted.

git branch -D <name>

git checkout:

Combine changes from different branches.

git checkout <branch>

Creates a new branch and makes it your current working branch.

git checkout -b <new-branch>

git merge:

Joins changes from one branch into another branch.

git merge <branch>

To abort the merge and try to go back to the pre-merge state. This can only be used after merge conflicts.

git merge --abort

Undo Changes

git reset:

Say you have just used the command git add. to stage all of your changes, but then you decide that you are not ready to stage those files. You could use the command git reset to undo the staging of your files.

Doing a hard reset can be extremely dangerous. With a hard reset, you run the risk of losing your local changes. There are safer ways to achieve the same effect. For example, you could run git stash, which will temporarily shelve or stash your current changes. This way, your current changes are kept safe, and you can come back to them if needed.

If you do a hard reset against an earlier commit, it’s the same as first doing a soft reset and then using reset --hard to reset your working tree. Thus, the following commands are equivalent:

git reset --hard HEAD~3  # Go back in time, throwing away changes
git reset --soft HEAD~3  # Set HEAD to point to an earlier commit
git reset --hard  # Wipe out differences in the working tree

Instead, use git stash

git stash
git checkout -b new-branch HEAD~3   # head back in time!

git revert:

This command makes a new commit which effectively rolls back a previous commit. Unlike the git reset command which rewrites your commit history, the git revert command creates a new commit which undoes the changes in a specific commit. Therefore, a revert command is generally safer than a reset command.


The Ultimate Git Workflow Template for Open Source Contributions

Embarking on a quest to contribute to open-source projects can be both exhilarating and daunting. Whether you're a seasoned developer or taking your first steps into the world of contributions, this is your only Git workflow template you need for successful open-source contributions.

  1. Fork the Repository: Visit the open-source project's repository on the hosting platform and click on the "Fork" button to create your copy of the repository.

  2. Clone Your Fork:

    bash
    git clone https://github.com/your-username/project-name.git
    cd project-name
    
  3. Add Upstream Remote (Note: The link is of originial repository):

    bash
    git remote add upstream https://github.com/original-owner/project-name.git
    
  4. Fetch Upstream:

    bash
    git fetch upstream
    
  5. Set Upstream:

    bash
    # Set up tracking for your main branch to the upstream repository
    git branch --set-upstream-to=upstream/main
    
  6. Create a Feature Branch:

    bash
    git checkout -b feature-name
    
  7. Make Changes Locally:

    • Implement the desired feature or fix.
    • Make small, focused commits.
  8. Keep Your Fork Updated:

    bash
    git fetch upstream
    git checkout main
    git merge upstream/main
    
    # This helps set up the tracking relationship between your local branch and the corresponding branch in the upstream repository.
    # This command is typically used after fetching changes from the upstream repository to make it easier to push changes and stay in sync.
    git branch --set-upstream-to=upstream/main
    
    • Setting up the tracking relationship (git branch --set-upstream-to) makes it easier to push changes to the correct branch in your fork. After this setup, you can typically use git push without specifying the remote and branch names.
    • While not strictly required, setting up tracking branches can streamline your workflow and reduce the likelihood of mistakes when pushing changes. It's a good practice to include this step, especially in collaborative or long-term development scenarios.
  9. Rebase Your Feature Branch::

    bash
    git checkout feature-name
    git rebase main
    
    • Resolve any conflicts if necessary.
  10. Push Changes to Your Fork:

    bash
    git push origin feature-name
    
  11. Create a Pull Request::

    • Go to your fork on the hosting platform.
    • Create a pull request from your feature branch to the original project's main branch.
    • Provide a descriptive title and details about your changes.
  12. Discuss and Iterate::

    • Engage in discussions with maintainers and other contributors.
    • Make changes based on feedback.
  13. Squash and Merge:

    • If needed, squash your commits into a more coherent set.
    • Work with maintainers to merge your changes.
  14. Clean Up:

    bash
    git checkout main
    git pull upstream main
    git branch -D feature-name
    

    Notes:

    • Always create a new branch for each feature or bug fix.
    • Keep commits focused and follow the project's coding conventions.
    • Regularly update your fork to minimize merge conflicts.
    • Be responsive to feedback and follow the project's contribution guidelines.

    This template is a general guideline. Always refer to the specific project's contribution guidelines and follow any additional steps or recommendations they provide.

Best Practices

  • Commit Often: Frequent, small commits make it easier to track changes and understand the project's history.
  • Use Branches Wisely: Create feature branches for new development and bug branches for fixing issues. Merge them back into the main branch when ready.
  • Write Meaningful Commit Messages: Clear and descriptive commit messages help collaborators understand the purpose of each change.
  • Pull Requests: When working in a team, use pull requests or merge requests to propose changes and discuss them before merging.
  • Regularly Update Your Repository: Pull changes from the main branch to keep your local repository up to date.

Conclusion

Git has become an indispensable tool for developers, enabling efficient collaboration and version control. By understanding its basic concepts, mastering essential commands, and following best practices, developers can navigate the seas of collaboration with confidence. Whether you're a solo developer or part of a large team, embracing Git can streamline your workflow and contribute to the success of your projects. Happy coding!