You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# π 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.
π 2. git status
git status
Description: Displays the state of the working directory and the staging area. It shows changes that are staged, unstaged, and untracked files.
β 3. git add
git add <file>
git add .
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.
π¦ 4. git commit
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.
π 5. git push
git push origin <branch>
Description: Pushes committed changes to the remote repository. Replace <branch> with the branch you are working on (e.g., main, dev).
β¬οΈ 6. git pull
git pull origin <branch>
Description: Fetches and integrates changes from the remote repository into your current branch. This keeps your local branch up-to-date with remote changes.
π 7. git branch
git branch
git branch <new-branch>
Description: Lists all branches in the repository. Use git branch <new-branch> to create a new branch.
Description: Switches to another branch or commit. Use -b to create and switch to a new branch.
π οΈ 9. git merge
git merge <branch>
Description: Merges changes from the specified branch into the current branch. Commonly used when integrating feature branches into main or dev.
π§Ή 10. git rebase
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.
βοΈ 11. git reset
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.
π 12. git log
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.
β¬οΈ 13. git fetch
git fetch origin
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.
β 14. git stash
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.
π§½ 15. git clean
git clean -fd
Description: Removes untracked files and directories from the working directory. Useful when you want to clean up your project directory.
ποΈ 16. git remote
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).
π 17. git clone
git clone <url>
Description: Clones a repository from a remote server to your local machine. This command creates a full copy of the project including its history.
π 18. git diff
git diff
Description: Shows the changes between your working directory and the last commit. This is useful for reviewing code before committing.
π¨ 19. git push --force
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.
π 20. git tag
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.
π§ 21. git cherry-pick
git cherry-pick <commit>
Description: Applies changes from a specific commit into your current branch. This is useful for selectively integrating changes.
π§Ή 22. git reflog
git reflog
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.
π Key Git Concepts:
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.