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
--globalflag: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 initCheck Hidden Files
ls -lartCreate Files
touch filename.ext ex: touch index.htmlCheck Repository Status
git status
Staging and Committing Changes
Add Files to Staging Area
Add a specific file:
git add index.htmlAdd all changes:
git add -AAdd 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.htmlRoll back all files:
git checkout -f
Viewing Logs and Comparing Changes
Check Commit History
git logShow detailed changes for the last commit:
git log -p -1Press
Qto exit the log view.
Compare Changes
Compare staged changes with the last commit:
git diff --stagedDo 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.txtForce remove a file (if errors occur):
git rm filename.txt -f
Ignore Files
Create a
.gitignorefile to specify files and folders to be ignored by Git:touch .gitignore
Working with Branches
Check Branches
git branchCreate a New Branch
git branch branch_nameSwitch Between Branches
Switch to an existing branch:
git checkout branch_nameCreate 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_namePull 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 stashApply the stashed changes:
git stash applyClone a Repository
git clone repository_urlRevert a Commit
git revert commit_hashView Remote Repositories
git remote -vFetch Changes Without Merging
git fetch origin branch_name
Tips
Always use descriptive commit messages to keep track of changes.
Use
.gitignoreto 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