๐ Git Command Essentials#
๐ Repository Management#
-
Create and Initialize Repositories:
git init- Initialize a new Git repository.git clone <repo-url>- Clone an existing repository.
-
Configure Git:
git config --global user.name "Your Name"- Set your Git username.git config --global user.email "you@example.com"- Set your Git email.
-
Check Repository Status:
git status- Display the current state of the working directory.git log- Show commit history.
-
Staging and Committing:
git add <file>- Stage changes for commit.git commit -m "Commit message"- Commit staged changes.git commit --amend- Modify the last commit message.
๐ Branch Management#
-
Creating and Switching Branches:
git branch <branch-name>- Create a new branch.git checkout <branch-name>- Switch to an existing branch.git switch <branch-name>- Alternative forgit checkout.
-
Managing Branches:
git branch -d <branch-name>- Delete a branch.git branch -a- List all branches (local and remote).git merge <branch-name>- Merge a branch into the current branch.
-
Tracking Remote Branches:
git fetch- Fetch changes from a remote repository.git pull- Fetch and merge changes from a remote branch.git push- Push changes to a remote branch.
๐ Version Control and History#
-
View Changes:
git diff- Show changes in the working directory.git diff <commit>- Compare the working directory with a specific commit.
-
Revert Changes:
git checkout -- <file>- Discard changes to a file.git reset HEAD <file>- Unstage changes.git revert <commit>- Create a new commit to undo a specific commit.
-
Inspect History:
git log- View commit history.git log --oneline- Compact history view.
๐ง Merging and Conflict Resolution#
-
Merging Branches:
git merge <branch-name>- Merge a branch into the current branch.
-
Conflict Resolution:
- When a merge conflict occurs:
- Edit conflicting files to resolve issues.
- Mark resolved files with
git add <file>. - Complete the merge with
git commit.
- When a merge conflict occurs:
-
Abort a Merge:
git merge --abort- Cancel an ongoing merge.
๐ Git Workflows#
-
Gitflow Workflow:
git checkout -b feature/<feature-name>- Start a new feature branch.git checkout develop- Switch to the develop branch.git merge feature/<feature-name>- Merge a feature branch into develop.git checkout main- Switch to the main branch.git merge develop- Merge develop into main for release.
-
Feature Branching Workflow:
- Each new feature or bug fix gets its branch.
- Branch naming convention:
feature/<name>orbugfix/<name>. - Merge branches back into
mainordevelopas appropriate.
๐ Remote Collaboration#
-
Remote Repository Management:
git remote add origin <repo-url>- Link a remote repository.git remote -v- View configured remote repositories.
-
Push and Pull:
git push origin <branch-name>- Push local changes to a remote branch.git pull origin <branch-name>- Fetch and merge remote changes.
-
Forking and Pull Requests:
- Fork a repository on GitHub or GitLab.
- Clone the forked repository locally with
git clone. - Push changes to a feature branch in the fork.
- Open a pull request to propose changes to the original repository.
๐งน Cleanup and Optimization#
-
Remove Untracked Files:
git clean -f- Delete untracked files.git clean -fd- Delete untracked files and directories.
-
Rebase:
git rebase <branch-name>- Reapply commits on top of another branch.
-
Squash Commits:
- Use
git rebase -ito interactively squash commits.
- Use
โค๏ธ Contributors#
- Curated by Ankush Patil.