In this blog, we will create our new repository in GitHub and perform the basic GitHub commands.
First of all, let's login into GitHub and create a new private repo called practice by clicking new in the top left corner of GitHub. It looks like this:
You can check all the configurations whether you want to keep your repository private or public.
After clicking the create repository, we can see the above page with the given instructions which we can perform in our local terminal or in any code editor like visual studio.
Let's go to our terminal and perform the following.
Configure User Credential
git config --global user.name “[name]” #use your github name
git config --global user.email “[email address]” #use the github email address
git init:
This command is used to initialize a new repository.
git init <"path"> #init in the directory where you have placed your codes
Clone the Repository:
Copy the repository's URL from the GitHub page and Use the git clone
a command followed by the repository URL.
git clone <"repository_link">
git add:
This command adds a file to the staging area.
git add . # for adding all the current files in the staging area
git add <"file_name"> # for adding only selected files in the repo
git commit:
This command records or snapshots the file permanently in the version history.
git commit -m "Initial commit"
git branch:
The git branch command is used to determine what branch the local repository is on.
git branch "feature1" # Creates a new branch named feature1
git branch -a # List all remote or local branch names
git branch -d "feature1" # deletes a branch named feature1
git status:
The status command is used to display the state of the working directory and the staging area.
git status
git push:
The command git push is used to transfer the commits or push the content from the local repository to the remote repository.
git push -u origin <"branch_name">
git pull:
The git pull command is used to fetch and merge changes from the remote repository to the local repository.
git pull <"branch_name"> <"repo_url">
Apart from these, you may face some problems while authenticating with GitHub due to which you cannot push your code into GitHub.
This is one issue that I faced while I tried to push into GitHub. For this, you can do the following steps:
Generate ssh key
command: ssh-keygen -t rsa (generate an RSA SSH key pair)copy the public key from the ~/.ssh/id_rsa.pub. This is the location where the public key is placed.
Paste on the GitHub SSH and GPG keys section. You can find this section by clicking on the profile then on settings and after that on the left side, you can see the SSH and GPG keys section. Click on the New SSH key and paste the key value you copied from ~/.ssh/id_rsa.pub.
Finally, after setting the ssh keys we can push our code into GitHub. We can see the following output when the code is pushed to GitHub successfully.
Therefore, we can conclude that we created the repository in GitHub and update the repository locally and pushed it into GitHub successfully.
Happy Learning !!