Git Commands Every Programmer Should Know
What is Git?
Git is by far the most widely used version control system in the world. It is a mature and actively maintained open source project originally developed in 2005 by Linus Torvalds, the creator of Linux OS. It is an essential part of software development in agile and DevOps teams. Its distributed nature gives superior performance characteristics and allows developers the freedom to experiment locally and publish their changes when they’re ready for distribution.
One common criticism of Git is that it can be difficult to learn if you are a complete beginner. Some terminology in Git can appear strange to newcomers and users of other systems.
- 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.
1. Initialize — git init
Initialize an existing directory as a Git Repo.
2. Clone a Repository — git clone [url]
Retrieve and clone a repo stored at a hosted location via URL onto local machine.
4. Show Modified Files — git status
Show status of all files. Staged, unstaged or untracked.
5. Add Files to Staging — git add [file name]
Add a file as it looks now to your next commit.
6. Show differences — git diff
Differences of what is staged but not commited.
7. Commit — git commit -m [message]
Commits staged snapshot with a descriptive message.
8. Show Changes — git log
Show all the commits in the current branch history.
9. List All Branches — git branch
List the branches of your repository.
10. Create New Branch — git branch [branch name]
Create a new branch in the current commit.
11. Switch branches — git checkout [branch name]
Switch to another branch and checkout its contents.
12. Merging — git merge [branch]
Merge the branch into the current directory.
Git File Management
13. Remove file — git rm [file name]
Removes the specified file from the project and stages the change.
14. Move file — git mv [existing path] [new path]
Change an existing path file to a new location and stages the change.
15. Adding an Alias — git remote add [alias] [url]
Create a new connection to a remote repository. The alias can be used as a shortcut for the URL in other commands.
16. Uploading New Changes — git push [branch]
Transmit local branch commits to the remote repository.
17. Downloading New Changes — git pull
Fetch and merge any commits from the remote repository.
18. Unstage a File — git reset [file name]
Unstage a file while retaining the changes in the working directory.
19. Go Back in Time — git reset —hard [commit]
Clear staging area and go back to changes made in the repository from the specified commit.
20. Undo — git revert [commit]
Create new commit that reverts all changes done in the specified commit and apply it to the current branch.
21. Apply Changes to Another Branch — git rebase [branch]
Apply any commits of current branch ahead of specified one.