Open Source Know-Hows: A Technical Guide

Open Source Know-Hows: A Technical Guide

Learn Vital Git Commands and GitHub Workflows for an Efficient Work Process

Open source projects are all about working together, being transparent, and sharing contributions. To contribute effectively, it's important to get a handle on the technical side of things, like using Git commands and managing workflows.

This guide will walk you through the must-know Git commands, GitHub workflows, and handy terminal commands to make your work smoother and more enjoyable.

1. Setting Up Git

Install Git

Or optionally you can try installing with the terminal.

# Debian/Ubuntu
sudo apt update
sudo apt install git

# macOS
brew install git

# Windows (Git Bash is recommended for terminal support)
# Download Git from https://git-scm.com/downloads

Configuring Git

# Set up your identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Set default editor for commits (optional)
git config --global core.editor "vim"

2. Working with Repositories

Initialize and Clone Repos

# Initialize a new Git repository in the current directory
git init

# Clone a repository
git clone <repository_url>

Basic Repository Navigation

# List branches
git branch

# Switch branches
git checkout <branch_name>

# Create a new branch and switch to it
git checkout -b <new_branch>

3. Staging and Committing

Adding Changes

# Stage a specific file
git add <file_name>

# Stage all changes
git add .

Committing Changes

# Make a commit with a message
git commit -m "Commit message"

# Commit all staged changes with a detailed message in editor
git commit

4. Pulling and Pushing Changes

Pulling Latest Changes

# Pull from the remote repository
git pull origin <branch_name>

Pushing to Remote

# Push changes to the remote repository
git push origin <branch_name>

Handling Merge Conflicts

# See where conflicts occur
git status

# Edit conflicting files to resolve conflicts, then add and commit
git add <conflicted_file>
git commit -m "Resolved merge conflicts"

5. Git Diff, Logs, and Revert

Viewing Changes

# See unstaged changes
git diff

# See changes between commits
git diff <commit_id_1> <commit_id_2>

Viewing History

# Show commit history
git log

# One-line summary of commit history
git log --oneline

# Limit history to a certain number of entries
git log -n <number>

Undoing Changes

# Unstage a staged file
git reset <file_name>

# Undo the last commit (soft reset: keeps files, hard reset: removes files)
git reset --soft HEAD~1
git reset --hard HEAD~1

6. Branch Management

Merging Branches

# Merge a branch into the current branch
git merge <branch_to_merge>

Deleting Branches

# Delete a local branch
git branch -d <branch_name>

# Force delete a branch
git branch -D <branch_name>

7. Working with GitHub

Forking and Cloning

  1. Fork the repository on GitHub to your own account.

  2. Clone the forked repository locally:

     git clone <your_forked_repo_url>
    

Setting Up Upstream

# Add the original repository as upstream
git remote add upstream <original_repo_url>

# Fetch upstream changes
git fetch upstream

# Merge upstream changes into your local main branch
git checkout main
git merge upstream/main

Creating Pull Requests

  1. Push your changes to your forked repository.

     git push origin <branch_name>
    
  2. Go to GitHub and create a Pull Request from your branch to the main repository.

8. Advanced Tips

Stash Changes

# Stash changes to work on something else
git stash

# List all stashes
git stash list

# Apply the latest stash
git stash apply

Squashing Commits

# Squash last n commits interactively
git rebase -i HEAD~<n>

# Mark commits as "squash" to combine them

Amending Commits

# Amend the last commit
git commit --amend

Tagging Commits

# Add a lightweight tag
git tag <tag_name>

# Add an annotated tag
git tag -a <tag_name> -m "Tag message"

# Push tags to remote
git push origin <tag_name>

9. Useful Git Shortcuts

# Add and commit in one step
git commit -am "Message"

# Push changes with the force option
git push origin <branch> --force

# See summary of commit history
git log --graph --oneline --all --decorate

This technical guide focuses on practical Git commands and GitHub workflows that make open source contributions efficient. With these commands, you’re ready to navigate repositories, collaborate seamlessly, and make meaningful contributions to open-source projects.

If you're interested in open source projects, check out QuickBlox's open source initiatives. We invite you to join our Discord community to connect with other developers and stay updated. Also, follow us on our social media channels: