40 Useful Git Commands

RustcodeWeb
3 min readApr 30, 2024

--

Photo by Farhat Altaf on Unsplash

Git, a distributed version control system, has revolutionized the way developers collaborate on projects, track changes, and manage codebases. With its powerful set of commands, Git empowers developers to streamline their workflows, maintain code integrity, and facilitate seamless collaboration.

In this comprehensive guide, we’ll explore 40 essential Git commands that are indispensable for developers working with version control systems.

1. git init: Initialize a new Git repository in the current directory.

2. git clone [repository]: Clone an existing repository into a new directory.

3. git add [file]: Add file changes to the staging area.

4. git commit -m “message”: Commit staged changes with a descriptive message.

5. git status: Display the current state of the repository.

6. git diff: Show the difference between the working directory and the staging area.

7. git diff — cached: Show the difference between the staging area and the last commit.

8. git log: Display the commit history.

9. git branch: List all branches in the repository.

10. git checkout [branch]: Switch to a different branch.

11. git checkout -b [branch]: Create a new branch and switch to it.

12. git merge [branch]: Merge changes from a specified branch into the current branch.

13. git remote -v: List all remote repositories.

14. git remote add [name] [url]: Add a new remote repository.

15. git fetch [remote]: Fetch changes from a remote repository.

16. git pull [remote] [branch]: Fetch changes from a remote repository and merge them into the current branch.

17. git push [remote] [branch]: Push local changes to a remote repository.

18. git reset [file]: Unstage changes for a specific file.

19. git reset — soft [commit]: Reset the current HEAD to a specified commit, keeping staged changes.

20. git reset — hard [commit]: Reset the current HEAD to a specified commit, discarding all changes.

21. git revert [commit]: Create a new commit that undoes the changes introduced by a specified commit.

22. git rm [file]: Remove a file from the working directory and stage the deletion.

23. git mv [old file] [new file]: Rename or move a file and stage the changes.

24. git stash: Stash changes in the working directory for later use.

25. git stash list: List all stashed changes.

26. git stash apply: Apply the most recent stash to the working directory.

27. git stash pop: Apply the most recent stash and remove it from the stash list.

28. git cherry-pick [commit]: Apply the changes introduced by a specified commit to the current branch.

29. git rebase [branch]: Reapply commits from the current branch onto the specified branch.

30. git tag [name]: Create a lightweight tag at the current HEAD.

31. git tag -a [name] -m “message”: Create an annotated tag with a message.

32. git tag -l: List all tags in the repository.

33. git show [tag]: Display information about a specific tag.

34. git remote rm [name]: Remove a remote repository.

35. git clean -n: Show which untracked files would be removed.

36. git clean -f: Remove untracked files from the working directory.

37. git config — global user.name “[name]”: Set the global username for Git.

38. git config — global user.email “[email]”: Set the global email address for Git.

39. git config — global core.editor “[editor]”: Set the default text editor for Git.

40. git help [command]: Display help information for a specific Git command.

Conclusion:

Git commands are the building blocks of effective version control and collaboration in software development. By mastering these 40 essential Git commands, developers can efficiently manage code changes, navigate branches, collaborate with team members, and maintain code integrity throughout the development lifecycle. Whether you’re a beginner or an experienced developer, having a solid understanding of these commands will empower you to leverage Git effectively and enhance your productivity in managing codebases and contributing to projects.

--

--

No responses yet