0% found this document useful (0 votes)
42 views

Git-GitHub Cheat Sheet

Uploaded by

dineshp2522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Git-GitHub Cheat Sheet

Uploaded by

dineshp2522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Git-GitHub

Basic Setup Cheatsheet


git config --global user.name "Your Name"
# Set your Git username.

git config --global user.email "your.email@example.com"


# Set your Git email.

git config --list


# List all Git configurations.

Status & Logs


Initializing and Cloning
git status
git init # Show the current status of changes in the
# Initialize a new Git repository in your project. working directory.

git clone <repo-url> git log


# Clone an existing repository. # View commit history.

git log --oneline

Working with Changes # Show concise commit history.

git add <file> Branching & Merging


# Stage a specific file for commit.

git branch <branch-name>


git add .
# Create a new branch.
# Stage all changes in the current directory.

git checkout <branch-name>


git commit -m "Commit message"
# Switch to a specific branch.
# Commit changes with a message.

git checkout -b <branch-name>


git commit -am "Message"
# Create and switch to a new branch.
# Add and commit tracked files in one step.

git merge <branch-name>


git commit --amend
# Merge specified branch into the current branch.
# Edit the last commit message or add changes to it.

git rebase <branch-name>

Handling Merge Conflicts # Reapply commits on top of another base.

git rebase -i HEAD~<n>


git diff # Interactive rebase to edit commit history,
# Compare working directory changes. rearrange commits, modify commit messages,
or squash the last n commits
git diff <branch1> <branch2>
# Compare two branches. git branch -d <branch-name>
# Delete a local branch (use -D to force delete).
# Resolve conflicts: Open the files,
Spoorti Shetty
fix conflicts, then add and commit.
Remote Repositories
Undoing Changes git remote add origin <url>
# Link your local repository to a remote one.
git reset <file>
# Unstage a file. git remote -v
# List the remote repository URLs.
git reset --soft HEAD~1
# Undo last commit but keep changes staged. git remote set-url origin <new-url>
# Update the remote URL for the repository.
git reset --mixed HEAD~1
# Undo last commit, keep changes in the git remote rename <old-name> <new-name>
working directory (unstaged). # Rename a remote.

git reset --hard HEAD~1 git push -u origin <branch-name>


# Completely remove the last commit. # Push changes to the remote repository.

git revert <commit-id> git pull origin <branch-name>


# Create a new commit that undoes the # Pull changes from the remote branch.
specified commit.
git fetch
# Download updates from the remote without
Stashing Changes merging.

git fetch <remote>


git stash
# Fetch updates from a specific remote.
# Temporarily save changes.

git stash list


# View stashed changes.
Advanced Operations
git stash pop git cherry-pick <commit-id>
# Reapply stashed changes and remove them # Apply a specific commit from another branch.
from the stash list.
git cherry-pick <start-commit-id>^..<end-commit-id>
git stash apply # Cherry-pick a range of commits.
# Reapply stashed changes without removing them.
git tag <tag-name>
git stash clear # Add a tag to a commit.
# Remove all stashed entries.
git tag -d <tag-name>
# Remove a local tag.

Collaborating & git reflog


# View history of all changes (even uncommitted).
Pull Requests
git reflog show <branch-name>
git branch -a # Show reflog for a specific branch.
# List all branches, including remote.
git show <commit-id>
git push origin :<branch-name> # Show detailed info for a specific commit.
# Delete a remote branch.
git bisect start
# Creating a Pull Request: Go to your GitHub # Start bisecting to locate a bug.
repository, select your branch, and click Spoorti Shetty
“New Pull Request.”
GitHub Commands
Reviewing Changes (Optional with GitHub CLI)
git show <file>
gh repo create
# Display changes made to a specific file.
# Create a new GitHub repo from the command line.

git diff <commit-id1> <commit-id2>


gh repo clone <repo-url>
# Compare changes between two commits.
# Clone a GitHub repository.

gh pr create

Help Command # Create a pull request from the command line.

gh pr list
git help <command> # List open pull requests in the repository.
# Get detailed help for a specific command.
gh issue create
# Create a GitHub issue from the command line.

GitHub API (using curl)


curl -H "Authorization: token YOUR_TOKEN" https://api.github.com/repos/USERNAME/REPO_NAME/issues
# List issues in a repository.

Repository Management
Submodules &
and Information
Worktrees
git shortlog -s -n
git submodule add <repo-url> <path> # Summarize commits by author.
# Add a submodule.
git describe --tags
git submodule init # Get a readable name for a commit.
# Initialize submodules.
git blame <file>
git submodule update # Show who last modified each line of a file.
# Update submodules.
git grep "search-term"
git worktree add <path> <branch> # Search for a term in the repository.
# Create a new working tree for a branch.
git revert <commit-id1>..<commit-id2>
# Revert a range of commits.
Cleaning Up
git archive --format=zip HEAD -o latest.zip
# Archive the latest commit as a ZIP file.
git clean -f
# Remove untracked files.
git fsck
# Check the object database for integrity.
git clean -fd
# Remove untracked files and directories.

git gc --prune=now
Spoorti Shetty
# Clean up unnecessary files and optimize the local repository.
Best Practices and Common Workflows

Commit Often: Make frequent commits with descriptive messages to maintain a clear project
history.

Branch for Features: Create a new branch for each feature or bug fix to keep changes
organized and separate from the main codebase.

Use Meaningful Commit Messages: Write clear and concise commit messages that explain the
purpose of the changes.

Pull Regularly: Regularly pull changes from the remote repository to stay updated with the
latest changes and minimize merge conflicts.

Resolve Conflicts Promptly: Address merge conflicts as soon as they arise to avoid
complicating the integration process.

Review Pull Requests Thoroughly: Ensure thorough review of pull requests to maintain code
quality and facilitate knowledge sharing.

Tag Releases: Use tags to mark important milestones or releases in the project for easy
reference in the future.

Keep Your Branches Clean: Delete branches that are no longer needed after merging them
into the main branch to keep the repository organized.

Use Git Hooks for Automation: Utilize Git hooks to automate tasks, like running tests before
committing (pre-commit) or checking commit message formats. Hooks can help ensure code
quality and consistency.

Squash Commits Before Merging: Squash commits to combine related work into a single
commit before merging, especially for feature branches. This keeps the project history clean
and manageable.

Avoid Large Commits: Try to keep commits small and focused on a single change or fix. This
makes it easier to understand the history and isolate issues if something goes wrong.

Create Descriptive Branch Names: Use branch naming conventions that describe the
purpose, such as feature/login-form or fix/user-authentication-bug. This improves readability
and collaboration.

Keep the Main Branch Deployable: Always ensure that the main or production branch is
stable and deployable. This allows the project to be released or updated at any time.

Spoorti Shetty

You might also like