At the beginning of this post I am assuming that you are familiar with Git and its usage and purpose. So I won’t go much further about it.
Git has some basic commands that are often needed. I will briefly discuss them here. From this post we will learn about the following topics-
- Creating a new repository.
- Configuring for the host server.
- Adding something new to Git.
- Creating commits or checkpoints.
- Push to remote server.
- Pulling from remote server.
Creating a new repository
To create a new repository we first need to initialize it. For this, open Git Bash in the folder that needs to be brought under Git. Then type this command –
git init
Entering this command will immediately create a git repository in your local directory. If you have the option to view hidden files in that file directory, you will see a folder named .git created there. Basically all changes will be saved inside this folder. Never delete this folder as unnecessary. Deleting will destroy your repository.
Configuring for the host server
If you want to share your repository with everyone online and host it on different hosting service providers like GitHub or Bitbucket, you need to configure it locally. For this you need to configure it with GitHub or Bitbucket credential.
git config – -global user.name “your_name”
git config – -global user.email “your_email”
You can then go to GitHub or Bitbucket to generate an RSA Key for your computer or not. If you generate a key, you won’t need a password for subsequent pushes. If you don’t create a key, you will need a password every time. And you will find all the steps to create the Key on that website.
Adding something new to Git
Let’s say you have placed or edited some files in your directory. To check if you have changed anything or not, type the following command-
git status
That is, you want to know the status of your directory.
If you have changed something, you will see it, if you have not changed anything, you will see the message that you are up to date.
Let’s say you want to commit the changes you made to Git. For this you need to enter the following command –
git add .
Using a dot (.) after “add” here means that you want to add all changes to files in that directory to Git at once. Let’s say you want to add only one particular file but not others, then you need to replace dot with that file name.
git add filename
Creating commits or checkpoints.
We haven’t added any checkpoints to what we’ve done so far. For this we have to commit. Commit means we want to create a checkpoint. To commit, type the following command-
git commit -m “Your Message"
Here -m means I want to write a message. And enter the message you want to write inside double quotes. If the message is short but meaningful, it is easy to find later.
Commit generates a code. This code can be used to roll back to this state later.
Push to remote server
What we’ve seen so far is using Git on a local device. If you only want to use it locally then this part is not needed. I assume you want to host your repository on a remote server like GitHub or Bitbucket. Then you first need to go to that site and create a repository. All you need to do after creating the repository is given there as instructions. Just follow. I personally prefer Bitbucket to GitHub, so from Bitbucket itself –
git remote add origin git@bitbucket.org:your_username/your_repository_name
Now I need to push to bring my local directory online.
git push origin master
Here master means branch. You can create different brunches. If no separate branch is created then by default it remains as master. Basically this is the main branch.
Pulling from remote server
Now if you want to use this repository of yours from another computer or want to use someone else’s repository (where you have access) then you need to use the pull command.
The repository will be copied to the directory where you pull.
git pull origin branch_name
This way you can access from different places.
Thank you