Git Basics:
Initialize a Repository:
git init
Clone a Repository:
git clone <repository-url>
Check Status:
git status
Stage Changes:
git add <file> git add . # Stage all changes
Commit Changes:
git commit -m "Commit message"
View Commit History:
git log
Create a New Branch:
git branch <branch-name>
Switch Branches:
git checkout <branch-name>
Merge Branches:
git merge <branch-name>
Undo Changes (Unstaged):
git checkout -- <file>
Undo Changes (Staged):
git reset HEAD <file>
Undo Last Commit (Keep Changes):
git reset --soft HEAD~1
Undo Last Commit (Discard Changes):
git reset --hard HEAD~1
GitHub Basics:
Push Changes to Remote Repository:
git push origin <branch-name>
Pull Changes from Remote Repository:
git pull origin <branch-name>
Add Remote Repository:
git remote add origin <repository-url>
View Remote Repositories:
git remote -v
Create Pull Request:
Push changes to GitHub.
Go to the repository on GitHub.
Click on "Pull Requests".
Click on "New Pull Request".
Select the branches to compare.
Merge Pull Request:
Go to the Pull Request on GitHub.
Review changes.
Click on "Merge Pull Request".
Confirm merge.
Fetch Changes from Remote:
git fetch origin
View Branches:
git branch -a
Remove Remote Repository:
git remote remove <repository-name>
Sync Forked Repository:
Add upstream repository:
git remote add upstream <upstream-repo-url>
Fetch changes:
git fetch upstream
Merge changes into local branch:
git merge upstream/<branch-name>
Create and Apply Gitignore:
Create
.gitignore
file in root directory.Add files or patterns to ignore.
Save changes and commit.
View Gitignore Status:
git status --ignored
Advanced Git:
Rebase Branch:
git rebase <base-branch>
Interactive Rebase:
git rebase -i <base-branch>
Cherry-Pick a Commit:
git cherry-pick <commit-hash>
Stash Changes:
git stash
Apply Stashed Changes:
git stash apply
View Stash List:
git stash list
Drop Specific Stash:
git stash drop <stash-index>
Clean Untracked Files:
git clean -fd
View Differences Between Branches:
git diff <source-branch> <target-branch>
Resolve Merge Conflicts:
Open the conflicted file.
Manually resolve conflicts.
Add resolved files.
Commit changes.
This cheat sheet covers essential Git and GitHub commands for beginners and more advanced users. Use it as a quick reference guide to streamline your workflow and collaboration processes.
"Fuel my passion and support my journey by clicking 'Buy me a coffee' today!"
~Dipen : )