Installation and Setup
Download Git
Visit Git Downloads and install Git on your system.Set Up Your Identity
Configure your user name and email:git config --global user.name "Your Name" git config --global user.email "your_email@example.com"
To configure these settings only for the current repository, omit the
--global
flag:git config user.name "Your Name" git config user.email "your_email@example.com"
Check Current Configuration
git config --global user.name git config --global user.email
Repository Setup
Initialize a Repository
git init
Check Hidden Files
ls -lart
Create Files
touch filename.ext ex: touch index.html
Check Repository Status
git status
Staging and Committing Changes
Add Files to Staging Area
Add a specific file:
git add index.html
Add all changes:
git add -A
Add changes in a specific folder:
cd folder_name git add .
Commit Changes
git commit -m "Initial commit"
Rollback Changes
Roll back one file to the last committed state:
git checkout index.html
Roll back all files:
git checkout -f
Viewing Logs and Comparing Changes
Check Commit History
git log
Show detailed changes for the last commit:
git log -p -1
Press
Q
to exit the log view.
Compare Changes
Compare staged changes with the last commit:
git diff --staged
Do not forget to do stage changes before comparing:
git add -A
Reset Repository
Hard reset to the last commit:
git reset --hard
Managing Files and Ignoring Changes
Remove Files
Remove a specific file:
git rm filename.txt
Force remove a file (if errors occur):
git rm filename.txt -f
Ignore Files
Create a
.gitignore
file to specify files and folders to be ignored by Git:touch .gitignore
Working with Branches
Check Branches
git branch
Create a New Branch
git branch branch_name
Switch Between Branches
Switch to an existing branch:
git checkout branch_name
Create and switch to a new branch:
git checkout -b branch_name
Merge Branches
git merge branch_name -m "Merge message"
Pushing Changes to a Remote Repository
Push Changes
git push -u origin branch_name
Pull Changes
Fetch and merge changes from a remote repository:git pull origin branch_name
here is the overall command line Initialize Git Repository: If you haven’t already, initialize a Git repository and commit your changes:
git init
git add .
git commit -m “Initial commit”
Add Remote Repository: Add your GitHub repository as a remote:
git remote add origin https://github.com/
Push to GitHub: Push your code to the main branch of your GitHub repository:
git push -u origin main
Advanced Commands
Stash Changes
Temporarily save uncommitted changes:git stash
Apply the stashed changes:
git stash apply
Clone a Repository
git clone repository_url
Revert a Commit
git revert commit_hash
View Remote Repositories
git remote -v
Fetch Changes Without Merging
git fetch origin branch_name
Tips
Always use descriptive commit messages to keep track of changes.
Use
.gitignore
to prevent unnecessary files (like logs, environment files) from being tracked.Regularly pull changes from the remote repository to keep your local copy updated.
Pull with
--allow-unrelated-histories
:git pull origin main --allow-unrelated-histories