Git
Git
Basic Setup
git config --global user.name "Your Name"
# Set your Git username.
git status
git init # Show the current status of changes in the
# Initialize a new Git repository in your project. working directory.
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
gh repo create
# Display changes made to a specific file.
# Create a new GitHub repo from the command line.
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.
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 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.