# 🚀 Useful Git Commands
This guide lists essential Git commands with a brief description for each. These commands will help you manage your workflow effectively and maintain clean project history.
---
## 🔄 **1. git init**
```bash
git init
- Description: Initializes a new Git repository in the current directory. This is the first step to start tracking a project with Git.
- Description: Displays the state of the working directory and the staging area. It shows changes that are staged, unstaged, and untracked files.
- Description: Adds changes to the staging area. Use
git add . to add all changes in the directory, or specify a file to add changes from a specific file.
git commit -m "commit message"
git commit --amend
- Description: Creates a snapshot of the staged changes. Use
-m to add a message. The --amend option allows you to modify the previous commit.
- Description: Pushes committed changes to the remote repository. Replace
<branch> with the branch you are working on (e.g., main, dev).
- Description: Fetches and integrates changes from the remote repository into your current branch. This keeps your local branch up-to-date with remote changes.
git branch
git branch <new-branch>
- Description: Lists all branches in the repository. Use
git branch <new-branch> to create a new branch.
git checkout <branch>
git checkout -b <new-branch>
- Description: Switches to another branch or commit. Use
-b to create and switch to a new branch.
- Description: Merges changes from the specified branch into the current branch. Commonly used when integrating feature branches into
main or dev.
git rebase <branch>
git rebase -i HEAD~3
- Description: Moves or combines a sequence of commits to a new base commit. Use the
-i (interactive) option to edit commit history, squash, or reorder commits.
git reset <file>
git reset --hard <commit>
- Description: Unstages or removes changes from the working directory. Use
--hard to reset the entire history and working directory to a specific commit.
git log
git log --oneline --graph
- Description: Shows a detailed log of commit history. The
--oneline --graph option provides a more condensed and visual representation of the history.
- Description: Retrieves changes from the remote repository without merging them into the current branch. Useful for seeing what others have pushed before you integrate it locally.
git stash
git stash apply
- Description: Temporarily saves changes that are not ready to be committed. Use
git stash apply to retrieve the stashed changes.
- Description: Removes untracked files and directories from the working directory. Useful when you want to clean up your project directory.
git remote -v
git remote add origin <url>
- Description: Lists or adds remote repositories. Use
git remote add to link your local repository to a remote one (e.g., GitHub).
- Description: Clones a repository from a remote server to your local machine. This command creates a full copy of the project including its history.
- Description: Shows the changes between your working directory and the last commit. This is useful for reviewing code before committing.
git push origin <branch> --force
- Description: Forcefully pushes changes to the remote repository, overwriting the history on the remote. Use this with caution, especially when working with a team, as it can overwrite others' changes.
git tag <version>
git push origin <version>
- Description: Creates a tag to mark a specific point in your project’s history (e.g., release versions). Use
git push origin <version> to push the tag to the remote repository.
- Description: Applies changes from a specific commit into your current branch. This is useful for selectively integrating changes.
- Description: Shows a history of all Git commands executed in your repository. This can be very useful for recovering from mistakes, especially after resets or rebases.
- Rebase: Reapplies commits on top of another base commit, keeping the commit history linear.
- Merge: Combines changes from multiple branches without altering the commit history.
- Force-push: Pushes changes while overriding the history on the remote repository.