Before using Git, you need to set up your identity so that your commits are properly attributed to you.
git config --global user.email "you@example.com"
git config --global user.name "Your Name"If you want to set it only for a specific project, remove --global.
To verify your configuration:
git config --list- Create a new project directory:
mkdir my_project cd my_project - Initialize a Git repository:
git init
This creates a hidden .git folder that tracks changes in your project.
To check if your repository has been initialized:
git status- Add a specific file:
git add filename
- Add all files in the directory:
git add .
git commit -m "Your commit message"To check the current status of your repository:
git statusTo view commit history:
git log- Create a repository on GitHub, GitLab, or Bitbucket.
- Link your local repository to the remote repository:
git remote add origin <remote_url>
- Verify the remote repository:
git remote -v
- Push your local commits to the remote repository:
git push -u origin master
To download an existing repository from a remote server:
git clone <repository_url>To clone into a specific directory:
git clone <repository_url> my_directorygit reset --soft HEAD~1git reset --hard HEAD~1git reset HEAD filenamegit checkout -- filenamegit branch new-branchgit checkout new-branchgit checkout -b new-branchgit checkout master
git merge new-branchgit branch -d new-branchgit pull origin mastergit fetch origingit rebase origin/mastergit tag v1.0.0git push origin --tagsgit stashgit stash listgit stash popgit stash apply stash@{0}git stash drop stash@{0}| Command | Description |
|---|---|
git config --global user.name "Name" |
Set global username |
git config --global user.email "email" |
Set global email |
git init |
Initialize a new repository |
git add . |
Stage all changes |
git commit -m "message" |
Commit staged changes |
git remote add origin <url> |
Link remote repository |
git push -u origin master |
Push code to master branch |
git pull origin master |
Pull latest changes |
git branch new-branch |
Create a new branch |
git checkout new-branch |
Switch to a branch |
git checkout -b new-branch |
Create and switch to a branch |
git merge new-branch |
Merge a branch into master |
git branch -d new-branch |
Delete a branch |
git log |
View commit history |
git status |
Check current state |
git reset --soft HEAD~1 |
Undo last commit (keep changes) |
git reset --hard HEAD~1 |
Undo last commit (discard changes) |
git checkout -- filename |
Discard local changes |
git stash |
Stash changes |
git stash pop |
Apply stashed changes |
git stash list |
List all stashes |
git tag v1.0.0 |
Create a tag |
git push origin --tags |
Push tags to remote |
git fetch origin |
Fetch updates from remote |
git rebase origin/master |
Rebase local branch |
Now, you're ready to master Git like a pro! 🚀
