In this cheat sheet, we will be looking at basic Git commands that we need to write to start with Git.
First of all, what is Git ?
Git is an open source, distributed, version control system.
Keeping all the jargon aside, basically Git is an open-source tools that helps us in code management and enables an individual or a team to keep track of their code and collaborate in a project where each individual is working on a different feature.
Version Control System: It is a software that helps keeping track of the changes that are made to a code, which can be useful where projects have many developers and includes many features.
Now, let's start with the stuff you all are here for.
Basic Git Commands:
git init
git init // create a repository
git init
creates a new local Git repository or transforms current directory into a Git repository
After creating a local Git repository now you can add the files that require to be commited by using the next command.
git add
git add .
git add
adds the files that you created new or modified to the staging area before committing the code and pushing it to the remote repository.
- If you want to add a specific file to the staging area then you need to mention the name of the file after the
git add
command as shown below.
git add readme.md
the above command will add only the readme.md
file to the staging area in order to commit it and than push to the remote repository.
Staging area: It is a space where all the files that are to be committed are temporarily stored before committing them.
- If you want to add all the files that are there in the directory to the staging area then you need to put a
.
aftergit add
command as shown below.
git add .
Now the code changes are there in the staging area.
git status
git status
When in doubt
git status
git status
does not change anything in the directory nor commits anything to the repository.
git status
shows the current state of our local directory and staging area, like which files are available to commit, which files are changed and not added to staging yet, to which branch are we targeting and much more useful information.
It is always a good idea to do
git status
after each step to know the current status and avoid any error while committing.
git commit
git commit -m "i am committing"
Now comes the interesting part and the word that you would have been listening a lot if you have spent little time in the IT world and are not living under a rock. COMMIT
Commit is like capturing a photograph of the repository at that particular time when a commit was done on a repository. It stores the information of the repository of that particular moment and is helpful if we need to look back and see what changes we made, how we have progressed and if new changes are not proper and we need to move to older state, commit comes in handy.
Remember git commit
does not make any changes to the repository, it just captures the state of the repository to which we can refer in the future if required.
One thing you would have noticed -m "I am committing"
. -m
allows us to include commit message we give while making a commit and this is very important as it helps a programmer to understand and keep track of the changes that he or she might have made.
Now, as we have committed the changes and we are ready to push the code into the remote repository. Let's do that. BUT !! where will you be pushing the changes to, yes to the remote repository but how to link that repository to our local repository. For that we do the next step.
git remote
git remote add origin <your remote git repository HTTP/SSH>
git remote
connects the local repository to the remote repository. This step needs to be done only once when we are pushing to a remote repository. Once you are connected to the repository you don't need to repeat this step after every git commit
.
git push
git push -u origin master
git push
pushes all the commits of our local repository to the remote repository, we can also say that it updates the files in the repository with the latest changes that were committed on the local repository and now both the repositories have same code.
Now the changes that you had made will be available to all the other members or team mates and they will be able to see the changes in the code.
This is how commits are made and code is pushed to the remote repository.
These were just the basic steps that we do to work with Git, still there is much more that we are yet to explore.
Hope this helped in getting started with Git.