What is Git and GitHub?
- Git is a version control tool (software) to track the changes in the source code.
- GitHub is an online platform used for storing, tracking, and collaborating on software projects.
- It enables developers to upload their own code files and to collaborate with fellow developers.
- Git doesn’t require GitHub but GitHub requires Git.
How to install Git on windows:
Step 1: Installation
- Download the latest version of git.
- you can use this link for windows users to download git and choose 32 or 64 bit version as per your system configuration. After the file is downloaded, install it in the system.
- Once installed, select Launch the Git Bash (optional), then click on finish.
Step 2: Verify whether git is installed properly
- Check the Git version.
Syntax:
git --version
- Here I am using an ssh client called Mobaxterm.
Step 3: Let’s practice
- Create a local directory using the following command:
Syntax:
mkdir <directory name>
cd <directory name>
touch <file name>
ls
- After creating the sample text file, add content to the file by using the vi command.
- Save and close the file.
Step 4:
- The next step is to initialize the directory:
Syntax:
git init
Step 5:
- To check the status type the following command.
Syntax:
git status
Step 6:
- Add the “sample.txt” file by using the command.
Syntax:
git add <file name>
Step 7:
- Next, make a commit using the following command:
Syntax:
git commit -m "commit message"
- The first part of the command git commit tells Git that all the files staged are ready to be committed.
- The second part -m “first commit” is the commit message. -m is shorthand for message while the text inside the parenthesis is the commit message.
How to push a file to GitHub:
Step 1 – Create a GitHub account:
- To use GitHub first create an account on their website.
- For new users click on the sign up button and fill in the details to create a new account.
- Once we login to the account the page will shown as below:
Step 2 – Create a repository:
- You can click on the + symbol on the top right corner of the page then select “New repository”.
- After clicking on the new repository give your repository name then scroll down and click on “Create repository”.
- Once we have open the created repository the page will shown as below:
- Here I created the repository name is file1.
Step 3-Repository Authentication:
- There are two methods:
1.username and password
2.using SSH key
Method 1- Username and Password:
- This is the simplest to set up, but it has the disadvantage that you will have to re enter your user name and password every time you need to push your code.
- Earlier this method was used but now it is unsupported by git.
Method 2- Using SSH key:
- This is more complicated to set up, but it has the advantage that it works effectively (you never have to enter your username and password).
How to Connect your GitHub repository using SSH Key?
Step 1-Check for an existing SSH key:
- First, check if you’ve already generated SSH keys for your machine. Open a terminal and enter the following command.
- If you’ve already generated SSH keys, you should see output similar to this:
Note:
- SSH keys are always generated as a pair of public (id_rsa.pub) and private (id_rsa) keys.
Step 2-Copy your public key:
- To copy the public key use the command mentioned below.
Add your public key to GitHub:
- Go to your GitHub page.
- You can click on your account on the top right corner of the page and then click settings.
- In the settings page, on the left hand side click on the SSH and GPG Keys.
- Click the New SSH key button.
- Then give your key a title name and paste in your public (id_rsa.pub) key:
- Then click on Add SSH key.
- Finally, test your authentication with:
Syntax:
ssh -T git@github.com
- If you’ve followed all of these steps correctly, you should see this message:
Hi your_user_name! You've successfully authenticated, but GitHub does not provide shell acc
Step 4-Run the git remote add origin command
- With the GitHub SSH link saved to the clipboard in the folder that contains your local Git repository, open a terminal window and run the following git remote add origin command.
- As shown below you can get the repository URL from GitHub.
- This command will execute, but the system won’t provide any feedback to the terminal window. To verify that use git remote –v command and it will show that GitHub is the fetch and push targets of the local repository.
Syntax:
git remote add origin git@github.com:username/repository name.git
Step 5-Run git push command:
- The below mentioned command pushes your file from your local device to GitHub.
- You should get a response similar to this:
Syntax:
git push -u origin master
- After the git remote add and push commands complete, go back to your GitHub account and look at the contents of the recently created repository.
Step 6- Collaborators in GitHub:
- In GitHub, owner invite our team workers to become collaborators access to their working repositories.
- Being a collaborator, of a working repository you can pull the contents/code of the repository and push changes to the repository.
how to add collaborators:
- In working repository, click on the settings option and select the collaborators option on the left side.
- Once you click on the collaborators option it will ask for the GitHub account password to enter.
- After enter the password the page will shown as below and select the Add people option.
- In the search field, enter the collaborators GitHub account user name to add people and click on the add user name to this repository.
5. Once the above step(four) completed it will send the invitation mail to the collaborators email id. The collaborators have to accept the invitation.
Step 7- Collaborators Clone Owner Repository:
- The collaborators can clone the owner repository by using the following command.
Syntax:
git clone git@github.com:username/repository name.git
- After using this command, the owner remote repository has been successfully saved in the collaborators local repository.
Step 8-Conflict in Git:
- Conflicts generally arise when two or more collaborators are working in the same file (if one developer deleted a file while another developer was modifying it).
- In these cases, Git cannot automatically determine what are the changes happened in the file.
How to resolve conflict in Git?
- Git will mark the file as being conflicted and stop the merging process.
- It is then the developers’ responsibility to resolve the conflict.
- To resolve the above error by using the below command:
Syntax:
git pull
- The file now appears in a modified state. Let’s examine the file and see what’s modified.
- Here we have used the vi command to put out the contents of this file. We can see some strange new additions.
- <<<<<<< HEAD – Our changes started by this line.
- ====== – Center of the conflict.
- >>>>>>> – End of the content pulled from the GitHub.
- Now, to edit this file to remove all the conflict dividers and keep the required data/code(save and quit the file). You may have to discuss which lines should be preserved with the team member who’s also working on the same file.
- Once the file has been edited use git add file name to stage the new merged content.
- Git will see that the conflict has been resolved and creates a new merge commit to finalize the merge.
- After completing the above steps, the data/content can be successfully pushed to the remote repository and it will be shown as below:
Step 9-Git diff:
- This command is used to see the changes in the file(the red color line represent the deleted line and green color line represent the newly added line).
Syntax:
git diff
Step 10- Git log:
- The git log command is used to shows a list of all the commits made to a repository.
- This command is useful for displaying the history of a repository.
- This command lists each commit with its the author’s name and email, the date written, and the commit message.
- It will list the commit in reverse order, the most recent commit will shown in first.
Syntax:
git log
Step 11- Git Branch:
- The Git branch is used to make a snapshot of the main (master) branch. This branch can be used to make code changes as needed, without affecting the main code line/production.
- We can also say that branches create another line of development in the project.
- The default branch in Git is the master branch .
- Commands for managing a git branch:
- Create branch
- List branch
- Delete branch
- Switch branch
1)Create branch:
- You can create a new branch with the help of the git branch command. This command will be used as:
Syntax:
git branch <branch name>
2)List branch:
- You can List all of the available branches in your repository by using the following command.
- Either we can use git branch — list or git branch command to list the available branches in the repository.
Syntax:
git branch --list
or
git branch
- The symbol * is representing currently active branch.
3)Delete Branch:
- You can delete the specified branch(empty branch).
- In this command, Git prevents you from deleting the branch if it has unmerged changes.
- It is a safe operation.
Syntax:
git branch -d <branch name>
4)Switch Branch:
- You can switch between two branches with the git checkout command.
- To switch between the branches, below command is used:
Syntax:
git checkout -b <branch name>
Switch to master branch:
- You can switch to the master branch from any other branch with the help of below command.
Syntax:
git checkout master
Step 12-Pull Request(PR):
- A pull request in GitHub where a collaborators asks a owner of a Git repository to review code they want to merge into a project.
How to create Pull Request?
- Here we are going to see how to create pull request to move code from “lab3” branch to “master” branch.
- Before pull request the content of lab3 and master branch is,
- In the selected lab3 branch click on pull request option in the top and click the “New pull request”.
- After clicking the New pull request it will redirect to the next page in this select the compare column as lab3 branch(from which branch you have to pull the data).
- Click on the create pull request it will redirect to the next page in this select the “Merge pull request”
- After selecting this option it will show confirm merge and select the same option and the message will shown as below:
- Once the pull request successfully merged (from lab3 branch to master branch) the changes in the file will shown as below.