-
Check Git Version:
git --version
Confirms the installed Git version.
-
Set Global User Details:
git config --global user.name "Your Name" git config --global user.email "[email protected]"
These details are added to every commit you make.
-
Initialize a New Repository:
git init
Creates a new Git repository in the current directory.
-
Clone an Existing Repository:
git clone <repository-url>
Downloads a remote repository to your local machine.
-
Check Repository Status:
git status
Displays the state of your working directory and staging area.
-
Stage Changes for Commit:
git add <filename> # Stages a specific file git add . # Stages all files in the current directory
-
Commit Staged Changes:
git commit -m "Meaningful commit message"
-
View Differences Before Committing:
git diff
Shows changes made in the working directory compared to the staging area.
-
Clone a Repository:
git clone <repository-url>
Downloads the repository to your system.
-
Make Changes in Files: Edit files in your working directory.
-
Stage Changes:
git add <filename>
Prepares the changes for the next commit.
-
Commit Changes:
git commit -m "Descriptive message about changes"
-
Show Commit Logs:
git log
Displays the commit history with details like commit ID, author, and date.
-
Filter Logs:
git log -3 # Shows the last 3 commits git log --oneline # Shows a summary of commits (one-line format) git log --stat # Lists files modified in each commit git log -p # Shows detailed differences in commits
-
Inspect a Specific Commit:
git show <commit-id>
Displays details of a specific commit, including changes made.
-
View Branches:
git branch
Displays a list of branches; the current branch is highlighted.
-
Create a New Branch:
git branch <branch-name>
Creates a new branch from the current branch.
-
Switch to a Branch:
git checkout <branch-name>
-
Create and Switch to a New Branch:
git checkout -b <branch-name>
-
Add and Commit in a Single Command:
git commit -am "Commit message"
-
Merge Branch into Current Branch:
git merge <branch-name>
-
Resolve Merge Conflicts:
- Fix conflicts manually by editing files.
- Stage the resolved files, then commit.
OR - Abort the merge process:
git merge --abort
-
Delete a Branch:
git branch -d <branch-name>
-
Create a Tag:
git tag -a <tag-name> <commit-id> -m "Tag description"
-
Delete a Tag:
git tag -d <tag-name>
-
Stash Uncommitted Changes:
git stash
Temporarily saves changes and cleans the working directory.
-
View Stashed Changes:
git stash list
-
Apply Stashed Changes:
git stash apply
-
Amend the Last Commit:
git commit --amend
Updates the last commit with new changes.
-
Revert a Commit:
git revert <commit-id>
Creates a new commit that undoes changes from a specific commit.
-
Reset to a Previous Commit:
git reset [--soft | --mixed | --hard] <commit-id>
- --soft: Keeps changes staged for commit.
- --mixed: Keeps changes in the working directory but unstages them.
- --hard: Discards all local changes.
-
Ignore Files:
- Create a
.gitignore
file to exclude files and directories from being tracked. - Example
.gitignore
entry:*.log /node_modules/ secret.txt
- Create a
-
Restore a File to the Last Commit:
git restore <filename>
Reverts the file to its last committed state.
-
Setup Git (Local):
git config --global user.name "userName" git config --global user.email "[email protected]"
-
Prepare Repository:
- On GitHub: Create an empty repository (no README or
.gitignore
). - On Local:
git init # Initialize local repo git add . # Stage all files git commit -m "Initial commit" # Commit changes git remote add origin <repository-url> # Link GitHub repo
- On GitHub: Create an empty repository (no README or
-
Push to GitHub:
git push -u origin master
- Enter your GitHub username:
userName
- Enter your Personal Access Token (PAT) as the password.
- Enter your GitHub username:
-
PAT Setup (if needed):
- Go to GitHub Settings > Developer Settings > Personal Access Tokens.
- Generate a new token with
repo
scope and use it instead of your GitHub password.
-
Verify Push:
Check your GitHub repository for the uploaded files and commit.
Tip: Use git remote -v
to verify the remote URL if you encounter issues.