Skip to content

Instantly share code, notes, and snippets.

@mahrous-amer
Created January 13, 2025 03:59
Show Gist options
  • Save mahrous-amer/1534c5bdeb729171588c10d13555a6b3 to your computer and use it in GitHub Desktop.
Save mahrous-amer/1534c5bdeb729171588c10d13555a6b3 to your computer and use it in GitHub Desktop.
This cheat sheet serves as a quick reference guide for developers of all levels. It covers essential commands for version control, including file comparison (diff and patch), repository management, committing, branching, remote operations, and security features like GPG signing. It also includes tips for viewing logs, handling file changes acros…

Git and GitHub

Diff and Patch

diff

diff is used to find differences between two files. For easier usage, combine it with -u:

diff -u file1 file2

diff -u

Compares two files line by line, displaying the differences side by side:

diff -u original.txt updated.txt

patch

patch applies file differences to another file. Example:

  1. Save differences as a .diff file:
    diff -u original.txt updated.txt > changes.diff
  2. Apply the patch:
    patch original.txt changes.diff

References

Git Basics

Initial Setup

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config -l  # Check current configuration

GPG Signing

Enable GPG signing for commits to enhance security:

  1. Generate a GPG key:
    gpg --full-generate-key
  2. Add your GPG key to Git:
    gpg --list-secret-keys --keyid-format=long
    git config --global user.signingkey <key_id>
    git config --global commit.gpgsign true
  3. Export your GPG key for public sharing:
    gpg --armor --export <key_id>
  4. Verify GPG signing with:
    git commit -S -m "Your message"

Creating a Repository

git init  # Initialize a new repository
git add file.txt  # Add a file to the staging area
git rm file.txt  # Remove a file from the repository

Committing Changes

git commit  # Commit changes (opens a text editor for the message)
git commit -m "Your commit message"  # Commit with a message inline
git commit -a  # Stage tracked changes and commit them in one step
git commit --amend  # Modify the last commit

Checking Status and Logs

git status  # Show the status of the working directory
git log  # View commit history
git log -p  # Show commits with changes (patch format)
git log --graph --oneline --all  # View commit tree in one line
git log --stat  # Display added/deleted statistics per file

Viewing Changes in Other Branches

git diff <branch1> <branch2>  # Compare two branches
git diff <branch>:<file>  # Compare a file from another branch

Differences

git diff  # Show differences between working directory and index
git diff --staged  # Show differences between staged files and last commit
git add -p  # Interactively choose changes to stage

Undoing Changes

git reset HEAD <file>  # Unstage a file
git reset -p  # Interactively reset changes
git revert <commit_id>  # Create a new commit to undo a previous one
git commit --amend  # Amend the last commit

Using Remote Repositories

Cloning and Synchronizing

git clone <URL>  # Clone a remote repository
git pull  # Fetch and merge changes from the remote repository
git push  # Push commits to the remote repository

Managing Remotes

git remote -v  # View configured remotes
git remote show origin  # View details of a remote repository

Branches

Managing Branches

git branch  # List branches
git branch <name>  # Create a new branch
git branch -d <name>  # Delete a branch
git branch -D <name>  # Force delete a branch

Switching and Merging

git checkout <branch>  # Switch to a branch
git checkout -b <branch>  # Create and switch to a new branch
git merge <branch>  # Merge a branch into the current branch
git merge --abort  # Abort a merge in case of conflicts

Viewing Branch History

git log --graph --oneline  # View branch commit history in graph form
git branch -r  # List remote branches

SSH and Credentials

Setting Up SSH

Other Useful Commands

Moving and Renaming Files

git mv <old-name> <new-name>  # Rename or move a file

Fetching Updates

git fetch  # Download changes from the remote repository

Skipping the Staging Area

git commit -a -m "Message"  # Stage and commit in one step
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment