GIT Cheat Sheet > Developer's Old Friend
Manager for your Project's Versions and Source Code
Before heading towards the content about GIT, let's first understand what generates the requirement for a Tool like GIT.
Requirement for a Tool like GIT
While working on a Software Engineering Project whether it is a Web Application, Backend Server, or Mobile Application. There is not just a single developer working on it, there is a team of developers working on a project.
While working in a team there was a lot of time consumed to manage access of Code Base to these developers. Even when it comes to integration of new features into the current Product it was difficult because the machines of developers are different and lots of time is consumed in these activities of Code Merging and Feature Integration or maintaining the Product development Cycle.
Tools like GIT is meant to solve the above problems and provide developers with ease of integration and merging of new features.
What is GIT?
GIT is like a manager for your Product, It actually manages your Product's Multiple Versions, it manages your Product's Source Code Accessibility and Source Code Management.
GIT is a very useful and majorly used tool as Distributed Version Control System and as a Source Code Management Tool. This tool can be used in order to send your code to the deployment server even it can also be used in order to manage a Team Building Project.
As this is a GIT Cheat Sheet let's move toward the commands that we use while developing and shipping our code.
GIT Commands
git init
: The git init command is used to create a new blank repository. It is used to make an existing project a Git project. Several Git commands run inside the repository, but the init command can be run outside of the repository.git status
: This command is mainly used in order to check out the status of our changes in tracked or untracked files in the current branch of the GIT repository.git diff
: This command will show all the changes in the current repository so that we can have a look or review our changes before committing our changes in our branch.git add <path to file>
: This will add new untracked files or changes in previously tracked files under git for committing or saving the new changes under the Git environment. For examplegit add ./project/
git commit -m <Message of Commit>
: This command is mainly used for permanently saving our previously added changes in our repository under the current branch. For examplegit commit -m "Added Changes for NavBar"
git branch
: This command will list all the branches on our current local repository and will highlight our current branch in our repository.git branch <new branch>
: This command will create a new branch if a new branch does not exist with the provided name in our environment. For examplegit branch Feature2
git checkout <branch>
: This command will make a switch from our current branch to the provided branch name in the command if the branch exists in the environment. For Examplegit checkout Feature2
git merge <branch>
: Sometimes we want to import changes from one branch into another branch. For this, we perform the merging of two branches. So in order to merge one branch into our current branch, we usegit merge Feature2
. This will take merge new changes from Feature2 Branch into our current branch.git reset --hard HEAD~1
: Sometimes there is a requirement where we need to undo one commit for this we can usegit reset --hard HEAD~1
this will undo 1 commit.git clean
: This command will remove untracked files from our current branch. We can usegit clean -f -d
here-f
specifies that only Files should be removed and-d
specifies that only Directories should be removed. In our case, because we've usedgit clean -f -d
it will remove all untracked files and untracked directories from the current repository.git push <remote> <branch>
: This command is mostly used command as this command will push your latest commits on your remote source in your specified branch.git push origin Feature2
. This command will push your latest changes on remote branch Feature2. If this branch does not exist on the remote repository then GIT will create a new branch with the name Feature2 and will push these changes into it.git pull <remote> <branch>
: In order to take changes from remote repository we mainly uses this command. This command will merge all the changes from the remote repository into your current branch. It is not necessary that in order to take a pull from master/main branch you should be in master/main branch in local as well. You can directly take a pull from this branch in the Feature2 branch as well i.e in any other branch as well. This will just merge your branch with the remote branch from which you are taking a pull.git pull <remote> <branch> --rebase
: Yes so here comes the new and interesting part of learning what will happen when we use the--rebase
option with this git pull command. So adding --rebase option with your Pull Command will not merge remote changes with your current changes rather it will pull changes from the remote branch and will put your changes/commits on top of those commits which we've received from the remote branch.
I hope that the above commands and explanation helped you to learn something new and clear some of your doubts and I hope that you will now start merging and rebasing your branches as per your requirement.