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

Git

This document provides a comprehensive guide on using Git and GitHub, covering basic setup commands, repository management, undoing changes, and advanced features like pull requests and submodules. It also includes best practices for effective version control, such as committing often, using meaningful messages, and keeping branches organized. Additionally, it highlights the importance of regular updates and conflict resolution to maintain code quality and project stability.

Uploaded by

įs mä
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)
3 views

Git

This document provides a comprehensive guide on using Git and GitHub, covering basic setup commands, repository management, undoing changes, and advanced features like pull requests and submodules. It also includes best practices for effective version control, such as committing often, using meaningful messages, and keeping branches organized. Additionally, it highlights the importance of regular updates and conflict resolution to maintain code quality and project stability.

Uploaded by

įs mä
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
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.

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


# Show concise commit history.

git add <file>


# 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>
# Reapply commits on top of another base.

git rebase -i HEAD~<n>


git diff
# Compare working directory changes. rearrange commits, modify commit messages,

git diff <branch1> <branch2>


# Compare two branches.
# Delete a local branch (use -D to force delete).
# Resolve conflicts: Open the files,
fix conflicts, then add and commit.
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
merging.

git fetch <remote>


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

git stash list


# View stashed changes.

git cherry-pick <commit-id>


git stash pop
# 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.

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
“New Pull Request.”
GitHub Commands

gh repo create
# Display changes made to a specific file.
# Create a new GitHub repo from the command line.

gh repo clone <repo-url>


# Compare changes between two commits.
# Clone a GitHub repository.

gh pr create
# 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.

curl -H "Authorization: token YOUR_TOKEN" https://api.github.com/repos/USERNAME/REPO_NAME/issues


# List issues in a repository.

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.

git archive --format=zip HEAD -o latest.zip

git clean -f # Archive the latest commit as a ZIP file.

# Remove untracked files.


fsck
# Check the object database for integrity.
git clean -fd
# Remove untracked files and directories.

git gc --prune=now
# Clean up unnecessary files and optimize the local reposit ory.
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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy