Git is a distributed version control system used for tracking changes in source code during software development. It allows multiple developers to collaborate efficiently while keeping a history of modifications.
Download Git from git-scm.com.
Run the installer and follow the default settings.
Verify installation by running:
git --version
Install Git using Homebrew:
brew install git
Verify installation:
git --version
Install Git using package manager:
sudo apt install git # Debian/Ubuntu sudo yum install git # CentOS/RHEL sudo pacman -S git # Arch Linux
Verify installation:
git --version
Set your user details:
git config --global user.name "Your Name" git config --global user.email "you@example.com"
Check configuration:
git config --list
git init
This command initializes a new Git repository in the current directory.
git clone <repository_url>
Clones an existing repository to your local machine.
git status
Shows the current state of the working directory and staging area.
git add <filename>
Adds a specific file to the staging area.
git add .
Adds all changes to the staging area.
git commit -m "Commit message"
Records changes to the repository with a descriptive message.
git push origin <branch_name>
Uploads local commits to a remote repository.
git pull origin <branch_name>
Fetches and merges updates from a remote repository.
git branch <new_branch_name>
Creates a new branch.
git checkout <branch_name>
Switches to the specified branch.
git switch <branch_name>
Newer alternative to git checkout
.
git merge <branch_name>
Merges the specified branch into the current branch.
git branch -d <branch_name>
Deletes a local branch.
git reset --soft HEAD~1
Moves the last commit back to the staging area.
git reset --hard HEAD~1
Deletes the last commit permanently.
git revert <commit_id>
Creates a new commit that undoes the specified commit.
git remote add origin <repository_url>
Links the local repository to a remote repository.
git remote -v
Displays the remote repositories linked to your local repository.
git fetch origin
Downloads updates from a remote repository but does not merge them.
git stash
Temporarily saves changes without committing them.
git stash list
Lists all stashed changes.
git stash apply
Applies the most recent stash.
git stash drop
Removes the most recent stash.
git log
Shows a history of commits.
git log --oneline --graph --all
Displays a simplified commit history.
git rebase <branch_name>
Moves your branch to the latest commit of another branch.
git rebase -i HEAD~3
Allows you to combine multiple commits into one.
git cherry-pick <commit_id>
Applies a specific commit from one branch to another.
Git hooks are scripts that run at different stages of the Git workflow.
Pre-commit hook: Runs before a commit is finalized.
Post-commit hook: Runs after a commit is finalized.
Example of a pre-commit hook (.git/hooks/pre-commit
):
#!/bin/sh echo "Running pre-commit hook"
Make the script executable:
chmod +x .git/hooks/pre-commit
This guide covers the essential and advanced Git commands. Git is a powerful tool that enables efficient collaboration, version tracking, and code management. The more you practice, the better you'll become at using Git effectively.