Git cheat sheet required for every developer
Git is one of the most popular and widely used version control systems. We use git on a day-to-day basis and are familiar with the most common commands and features. This article describes the commands and features which can make a developer’s life easier.
Before we begin, I write one line definitions of the terminologies used in git:
- Repository: A collection of files of a project or module is a repository (or repo in short). They are of two types:
a. Local: The one which is on the developer’s system.
b. Remote: The one which is on the Github server. - Branch: Consider the repository as a tree. A branch is independent code from other branch. The branches help you to divide the code or arrange the code for different purposes (usually).
- Commit: A commit represents the version of the code. For example if I have a code written of 10 lines and I add 2 more lines to it I will perform a commit and there would be 2 revisions of the code. With the help of this I can keep track of what I changed and when and also have the capability to revert my changes.
- Pull Requests: These are the requests created by a person while merging the code from one branch to another branch. These can be reviewed by other people as well.
These were the terminologies which I wanted to state here. Now let’s move to the commands which are used often.
I am writing the commands in the order in which they are used and would try to explain in the shortest way possible.
Starting with:
- git init
‘init’ as the name suggests — initializing. This command initializes an empty repository into the system. This repository is a local repository and would be connected with remote repository to interact with the server. - git remote
This command is used to access the connection between the remote and local repository. On running this you will get the list of remote names connected to this local repository.
git remote add origin <url-of-the-repository.git>
Above command is used to add a new remote repository into the system ‘origin’ being the remote name.
3. git config
Is used to define configuration for git. Use (- -global) for changing global git settings. It is also used to add alias to commands. This is useful when there are longer commands.
git config --global alias.co commit
This adds alias ‘co’ to commit command. Running ‘git co’ will run git commit.
4. git clone
Is used to clone an existing repository. If we want to use an existing repository, we need to clone it first.
git clone <link-to-the-repo.git>
5. git fetch
Is used mostly when you are working collaboratively with other people. It fetches the changes and details on the repository. If run without arguments, it fetches everything including branches and tags. Else we can specify a branch name to fetch only that branch.
git fetch ORgit fetch <remote> <branch>
6. git merge
Is usually used to merge different branch into current branch.
git merge <branch-name>
7. git pull
Is the combination of fetch and merge. Usually used to update local branch.
If run without arguments, pulls from the defined downstream in the config.
git pullORgit pull <remote> <branch-name>
8. git status
When we are running so many commands, at some time we might want to know where we are. This command tells us everything about our local repo.
9. git add
After we have done the changes, it is the time to tell git that we want to remember this file for the next commit. We add this file to our staged changes. These changes will go in the next commit.
git add <file-name>ORgit add .
(if you want to add all files)
10. git commit
Now, when we have staged the changes, git knows everything that we want to change, add or delete. It is the time to commit them.
Use the -m flag to add a custom message to your commit.
git commit -m '<your-message-here'>
This will generate a new commit for the changes. If there is a small change, you can also add your change to last commit by using amend flag.
git commit --amend
11. git push
The commit that we have created is now on the local repository. To move it to the remote repository, we will need to push the commit to the remote.
If run without arguments, it uses the upstream set in the config.
git push <remote> <branch>
12. git log
Used to get logs containing the last commits.
git log
13. git branch
Used to perform branch operations, such as create, list, delete branch.
git branch
14. git diff
Shows diff or changes in a particular file.
git diff abc.js
15. git checkout
Lets you to navigate between branches. You can also create a new branch by using -b flag using the current branch as base.
git checkout <branch-name>// To Create a new Branch
git checkout -b <new-branch-name>
Bonus
16. git revert
Used to revert to a previous commit. It reverts the code but brings the past changes into next commit. Thereby, keeping the unwanted changes in the history.
git revert <commit-id>
17. git rebase
Rebase is one of two Git utilities that specializes in integrating changes from one branch onto another. Rebase has powerful history rewriting features. It rewrites the entire history and helps to avoid conflicts.
git pull origin master
git checkout another_branch
git rebase master
It rewrites history of another_branch from the history of master branch.
18. git cherry-pick
Sometimes, a feature branch may go stale or a branch is not merged. In this case the commit can be picked and merged into working branch using cherry-pick.
git cherry-pick <commit-id>
I hope this article is helpful for you. Git is definitely one of the essential tools for a developer. Let me know your thoughts in the comments.