GitHub is an indispensable tool for developers worldwide. Mastering Git commands is crucial to efficiently managing your codebase and collaborating with others. Here, we've compiled 30 essential Git commands that will help you navigate your projects like a pro.
1. git init
Initializes a new Git repository in the current directory.
git init
2. git clone [url]
Clones a repository into a new directory.
git clone [url]
3. git add [file]
Adds a file or changes in a file to the staging area.
git add [file]
4. git commit -m "[message]"
Records changes to the repository with a descriptive message.
git commit -m "[message]"
5. git push
Uploads local repository content to a remote repository.
git push
6. git pull
Fetches changes from the remote repository and merges them into the local branch.
git pull
7. git status
Displays the status of the working directory and staging area.
git status
8. git branch
Lists all local branches in the current repository.
git branch
9. git checkout [branch]
Switches to the specified branch.
git checkout [branch]
10. git merge [branch]
Merges the specified branch's history into the current branch.
git merge [branch]
11. git remote -v
Lists the remote repositories along with their URLs.
git remote -v
12. git log
Displays commit logs.
git log
13. git reset [file]
Unstages the file, but preserves its contents.
git reset [file]
14. git rm [file]
Deletes the file from the working directory and stages the deletion.
git rm [file]
15. git stash
Temporarily shelves (or stashes) changes that haven't been committed.
git stash
16. git tag [tagname]
Creates a lightweight tag pointing to the current commit.
git tag [tagname]
17. git fetch [remote]
Downloads objects and refs from another repository.
git fetch [remote]
18. git merge --abort
Aborts the current conflict resolution process and tries to reconstruct the pre-merge state.
git merge --abort
19. git rebase [branch]
Reapplies commits on top of another base tip, often used to integrate changes from one branch onto another cleanly.
git rebase [branch]
20. git config --global user.name "[name]" and git config --global user.email "[email]"
Sets the name and email to be used with your commits.
git config --global user.name "[name]"
git config --global user.email "[email]"
21. git diff
Shows changes between commits, commit and working tree, etc.
git diff
22. git remote add [name] [url]
Adds a new remote repository.
git remote add [name] [url]
23. git remote remove [name]
Removes a remote repository.
git remote remove [name]
24. git checkout -b [branch]
Creates a new branch and switches to it.
git checkout -b [branch]
25. git branch -d [branch]
Deletes the specified branch.
git branch -d [branch]
26. git push --tags
Pushes all tags to the remote repository.
git push --tags
27. git cherry-pick [commit]
Picks a commit from another branch and applies it to the current branch.
git cherry-pick [commit]
28. git fetch --prune
Prunes remote tracking branches no longer on the remote.
git fetch --prune
29. git clean -df
Removes untracked files and directories from the working directory.
git clean -df
30. git submodule update --init --recursive
Initializes and updates submodules recursively.
git submodule update --init --recursive
Conclusion
These 30 Git commands are essential for any developer looking to effectively manage their projects on GitHub. Whether you're initializing a new repository, managing branches, or pushing your latest changes, mastering these commands will make your development workflow smoother and more efficient. Happy coding!
By familiarizing yourself with these commands, you can handle most Git operations with ease, allowing you to focus more on writing great code and less on managing your repository.