Git Commands Every Programmer Should Know

Photo by Yancy Min on Unsplash

What is Git?

Common Terms

  • Version Control: A system that records changes to a file or set of files over time so that you can recall specific versions later.
  • Repository (Repo): A collection of commits, branches and tags
  • Branch: A version of the repository that diverges from the main working project. Can be used as a new version of a repo or experimental changes.
  • HEAD: It is a reference variable used to denote the most current commit of the repository in which you are working.
  • Master: The primary branch of all repositories.
  • Merge: Taking the changes from one branch and adding them into another, traditionally the master branch.
  • Remote: A copy of the original branch.

Git Commands

Photo by Luke Chesser on Unsplash

Git Basics

1. Initialize — git init

2. Clone a Repository — git clone [url]

4. Show Modified Files — git status

5. Add Files to Staging — git add [file name]

6. Show differences — git diff

7. Commit — git commit -m [message]

8. Show Changes — git log

Git Branches

9. List All Branches — git branch

10. Create New Branch — git branch [branch name]

11. Switch branches — git checkout [branch name]

12. Merging — git merge [branch]

Git File Management

13. Remove file — git rm [file name]

14. Move file — git mv [existing path] [new path]

Remote Repositories

15. Adding an Alias — git remote add [alias] [url]

16. Uploading New Changes — git push [branch]

17. Downloading New Changes — git pull

Undo Changes

18. Unstage a File — git reset [file name]

19. Go Back in Time — git reset —hard [commit]

20. Undo — git revert [commit]

Change History

21. Apply Changes to Another Branch — git rebase [branch]



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store