GIT basic commands to push code on GitHub.
What is GIT?
Git is a tool used for source code management. It is a free and open-source version control system used to handle your projects. Git is used to tracking changes in the source code, enabling multiple developers to work together on non-linear development
- This is the first step in creating a repository. After running git init, adding and committing files/directories is possible.
git init
git init
This is the first step without this step you cannot use git commands.
- The git add command is used to add file to staging.
Now, what is the meaning of staging
Staging means now git will track all the files. If you do some changes in the file then by writing the command git status you can see git will tell you that this particular file is changed. After using the command git add .
will add that file to staging.
git add .
If you want to add a single file then you can use below command
git add filename
Now, commits comes
What are commits?
You can say that after moving all the files to staging then you will have to commit all the files. So that, you can push it to your git repository
git commit -m "First commit"
the -m
means message.
In the double quotes you have write a message. You can write anything there.
NOTE
The message should be a valuable message so that you can know that what changes happened in this commit by reading the message.
You can check all the commits by writing command git log
git log
if you are using git first time then after using the command git push
it will give you
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using
git remote add <name> <url>
and then push using the remote name
git push <name>
Here, you have to add your repository link.
You can get it from your GitHub repository.
This URL is of the repository where you want to push your code
Once you get the repository link use these commands
git remote add origin <URL>
git push origin master
If you use command git push origin master
it will push the code to the master branch.
But if, you use the command git push
. It gives to fatal error.
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
You just need to run git push --set-upstream origin master
command to setup upstream.
These commands will use only for first time only after adding these command there will be a connection between your local code and to your repository.
For the next time, you will only use the command git push
and it will push your code the repository with whom it's connected.
NOTE
If you want to push the code to the repository you have to commit all the changes done in your local code.
- git add .
- git status (To check the files are added)
- git commit -m "message"
- git push
Subscribe to my newsletter
Read articles from Shubham Chandel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by