Essential Git Commands Every Developer Should Know
Git is vital for software development, enabling version control and collaboration. Here are essential Git commands every developer should know to improve productivity and workflow.
1. git init
Initializes a new Git repository in the current directory.
git init
2. git clone
Clones an existing repository from a remote server to your local machine.
git clone <repository_url>
3. git add
Adds changes in the working directory to the staging area.
git add <file_or_directory>
4. git commit
Records the changes in the staging area to the repository.
git commit -m "Commit message"
5. git status
Displays the state of the working directory and staging area.
git status
6. git push
Uploads local repository content to a remote repository.
git push <remote_name> <branch_name>
7. git pull
Fetches and integrates changes from a remote repository to the local repository.
git pull <remote_name> <branch_name>
8. git branch
Lists, creates, or deletes branches.
List branches:
git branch
Create a new branch:
git branch <branch_name>
Delete a branch:
git branch -d <branch_name>
9. git checkout
Switches branches or restores working directory files.
Switch branches:
git checkout <branch_name>
Restore files:
git checkout -- <file_name>
10. git merge
Merges changes from one branch into another.
git merge <branch_name>
11. git log
Displays a log of commits.
git log
12. git reset
Resets the current branch to a specific state.
Soft reset:
git reset --soft <commit>
Hard reset:
git reset --hard <commit>
13. git stash
Temporarily saves changes in the working directory.
git stash
14. git remote
Manages tracked repositories.
List remotes:
git remote -v
Add remote:
git remote add <name> <url>
15. git fetch
Downloads objects and refs from another repository.
git fetch <remote_name>
16. git diff
Shows changes between commits, commit and working tree, etc.
git diff
17. git rebase
Reapplies commits on top of another base tip.
git rebase <branch_name>
18. git tag
Creates, lists, deletes, or verifies tags.
List tags:
git tag
Create a tag:
git tag <tag_name>
19. git config
Gets and sets repository or global options.
git config --global user.name "Your Name" git config --global user.email "you@example.com"
20. git blame
Shows what revision and author last modified each line of a file.
git blame <file_name>
Understanding and utilizing these essential Git commands can streamline your workflow, enhance collaboration, and ensure efficient version control. Happy coding!