GIT
GIT
1. Installation Commands:
Install Git:
o For Windows:
Download and install Git from the official website:
https://git-scm.com/.
o For Linux:
sudo apt-get install git
o For macOS:
brew install git
2. Configuration Commands:
Set Username:
git config --global user.name "Your Name"
This command sets the username for all repositories on your
system.
Set Email:
git config --global user.email "[email protected]"
This sets the email associated with your commits.
Check Configurations:
git config --list
Displays all Git configuration settings.
3. Repository Commands:
Initialize a New Repository:
git init
Creates a new empty Git repository in your project folder.
Clone a Repository:
git clone <repository-url>
Creates a local copy of a remote repository on your machine.
4. Basic File Operations:
Check the Status of Files:
git status
Shows which files are in the working directory, staged, or
ready to commit.
Add Files to Staging Area:
git add <file-name>
Moves files from the working directory to the staging area
(preparing them for commit).
Add All Files:
git add .
Adds all changes to the staging area.
Commit Changes:
git commit -m "Your commit message"
Commits the staged changes with a descriptive message.
Remove Files:
git rm <file-name>
Removes a file from the repository and stages the deletion.
5. Branching and Merging:
Create a New Branch:
git branch <branch-name>
Creates a new branch.
Switch to a Branch:
git checkout <branch-name>
Switches to an existing branch.
Create and Switch to a New Branch:
git checkout -b <branch-name>
Creates and switches to the new branch in one command.
Merge Branches:
git merge <branch-name>
Merges the changes from the specified branch into the
current branch.
Delete a Branch:
git branch -d <branch-name>
Deletes the specified branch.
6. Remote Repository Commands:
Add a Remote Repository:
git remote add origin <repository-url>
Links your local repository to a remote repository (often
named "origin").
View Remote Repositories:
git remote -v
Lists all the remote repositories linked to your local
repository.
Fetch Changes from Remote:
git fetch
Fetches changes from the remote repository without merging
them into your current branch.
Pull Changes from Remote:
git pull
Fetches and merges changes from the remote repository into
your current branch.
Push Changes to Remote:
git push
Sends your local commits to the remote repository.
7. Tracking and Viewing History:
View Commit History:
git log
Shows a list of all commits made in the repository.
View a Specific Commit:
git show <commit-hash>
Shows details of a specific commit, including changes and
metadata.
View Changes (Difference):
git diff
Shows differences between your working directory and the
staging area.
View Changes Between Branches:
git diff <branch1> <branch2>
Compares two branches.
8. Undoing Changes:
Unstage Files:
git reset <file-name>
Removes a file from the staging area (but keeps the changes
in the working directory).
Undo Last Commit (Keep Changes):
git reset --soft HEAD~1
Removes the last commit but keeps the changes in the
staging area.
Undo Last Commit (Discard Changes):
git reset --hard HEAD~1
Removes the last commit and discards the changes entirely.
Revert a Commit:
git revert <commit-hash>
Creates a new commit that undoes the changes made by a
specific commit.
9. Stashing Changes:
Stash Changes:
git stash
Temporarily saves your changes without committing them,
allowing you to switch branches.
Apply Stashed Changes:
git stash apply
Restores stashed changes to your working directory.
List Stashes:
git stash list
Shows a list of stashed changes.
Drop Stash:
git stash drop
Deletes a specific stash from the stash list.
10. Tagging:
Create a Tag:
git tag <tag-name>
Tags a specific commit with a version number or label.
List All Tags:
git tag
Shows all tags in the repository.
Push Tags to Remote:
git push origin --tags
Pushes all tags to the remote repository.
11. Git Ignore:
Create a .gitignore File: Inside this file, list the files or
directories that Git should ignore. Example:
/node_modules
.env
Git will now ignore these files in the repository.
12. Collaboration Commands:
Fork a Repository:
Forking is done via platforms like GitHub or GitLab,
where you copy someone’s repository to your account
to work independently.
Submit Pull Request:
After forking and making changes, you can submit a pull
request via GitHub to propose your changes to the
original repository.