Code Monkey home page Code Monkey logo

git-github's Introduction

Hands on Learning of Git and Github

Date: 14th October 2023

Speaker: @sahilyeole

Event page (register here): https://gdsc.community.dev/events/details/developer-student-clubs-g-h-raisoni-institute-of-engineering-and-technology-presents-hands-on-learning-of-git-and-github/

Topics to be covered

BONUS


Setting up git and github


Basic terminal commands for navigation

NOTE: For Windows users, use Git Bash for running the commands. It comes with the git installation. For this session, we will be using Git Bash for running the commands.

  1. pwd (Print Working Directory): Displays the current directory path.

    Example:

    $ pwd
    /home/user/documents
  2. cd (Change Directory):

    • To move forward (into a subdirectory):
      $ cd directory_name
    • To move backward (up one directory):
      $ cd ..
    • To move up two directories:
      $ cd ../..
  3. ls (List): Lists the files and directories in the current directory.

    Example:

    $ ls
    file1.txt file2.txt directory1 directory2

What is version control system?

  • Version control systems are software tools that help software teams manage changes to source code over time.
  • Version control systems help software teams work faster and smarter. Alt text
  • The types of VCS are:
    1. Local Version Control System
    2. Centralized Version Control System
    3. Distributed Version Control System
  • Git is the most well-known example of distributed version control systems. Alt text

Creating & cloning a repository

Creating a repository

  1. Open your github profile and go to Repositories tab.
  2. Click on New button. Alt text
  3. Give a name to your repositor.
  4. Make sure to check Add a README file option.
  5. Click on Create repository button. Alt text

Cloning a repository

  1. After creating a repository you will be redirected to the repository page. Or else you can access it from Repositories tab in your profile.
  2. Click on Code button.
  3. Copy the https link of your repository. Alt text
  4. Open your bash terminal and cd into your desired location (for me Developer folder).
$ cd Developer
  1. Open your bash terminal and enter git clone [your repo link]
git clone https://github.com/sahilyeole/Test.git  
  1. Now in your file manager you can see a new folder created with the name of your repository. You can also see this using ls command.

Alt text

  1. cd into that folder in terminal.
$ cd Test
  1. ls to see the files in that folder.
$ ls
README.md

It will show only README.md file as we have not added any other file yet.

Congrats you have successfully cloned your first repository.


Making changes

  1. Open the README.md file in your favorite text editor. (Ex: VS code or notepad) I'm vim user so I will use vim to open the file.
nvim README.md
  1. Add some text to the file and save it.
Hello world

Alt text

  1. Now go to your terminal and type git status command.
$ git status

Alt text git status gives the current status of the repository. It shows that README.md file is modified. And it says changes not staged for commit. It means that the changes are not yet added to the staging area.


Pushing changes to github

  1. Now we will add our modified file to the staging area using git add command.
$ git add README.md

Staging area is a place where we add the files which are ready to be committed. We can add multiple files to the staging area and commit them all at once.

Or if you want to stage all the files in the current directory of the repository then you can use git add . command.

$ git add .

Note:

. represents the current directory.

You can also restore the changes you have made to the file using git restore command. (You can ignore this command for now if you dont want to restore the changes.)

$ git restore README.md
  1. Enter git status command again to see the status of the repository.
$ git status

Alt text Now it says "Changes to be committed". It means that the changes are added to the staging area and are ready to be committed.

At this point you can verify your changes by using git diff command.

 $ git diff
  1. Now we will commit the changes using git commit command.

Commit is like a snapshot of the changes you have made. If we want to push the changes to github then we have to commit them first.

$ git commit -m "Added hello world"

-m flag is used to add a commit message. It is a good practice to add a meaningful commit message to every commit you make. It helps you to keep track of the changes you have made.

  1. Try git status command again.
$ git status

Alt text

Now it doesn't show any changes. It means that the changes are committed successfully. We can verify this by using git log command.

$ git log

git log shows the history of commits made to the repository. It shows the commit hash, author, date and commit message. Alt text

Note: Press q to exit from the log.

  1. We aren't done yet because these commits are currently stored only in our local machine. We have to push these commits to github so that everyone can see them.

We will use git push command to push the commits to github.

$ git push origin main

here origin is referring to the remote repository and main is the branch name.

Or you can also use

$ git push origin head

This will push to the current branch (head) you are on to the remote repository. In this case our head branch is main.

You can verify head branch here or even using git status command. Alt text

  1. After 5th step your commits are pushed to github. You can verify this by going to your repository page on github. Alt text

Summary of what we did so far

Alt text

Git branches and merging

Git branches are parallel versions of a codebase that allow developers to work on different features or fixes without impacting the main code.

  1. git branch to list all the available branches.
$ git branch

Currently there's only one branch named main. Press q to get out.

  1. git branch [branch name] to create a new branch.
$ git branch new_branch

now run git branch

Alt text

now our new branch new_branch has been added.

Notice the * it represents the head of the branch, meaning what branch our current local repository is pointing to. Currently it's on main branch lets switch it to new_branch. 3. git switch [branch name] to switch branches

$ git switch new_branch

Now our head is at new_branch meaning our repo is now pointing to new_branch Alt text Also, notice prompt in our terminal is showing new_branch. Alt text Also, git status will show this branch. 4. Now, we will do changes in this branch. The changes done in this branch are separate from other branches. I have edited README.md here. Alt text And now following above instructions lets add, commit and push the changes to github.

$ git add .
$ git commit -m "Update README"
$ git push origin head

Alternative for push command: git push origin new_branch.

Now our new branch has been pushed to github along with new changes. You can verify it here. Switch branch on github. Alt text Alt text This new branch is having the changes we have made in this branch.

Merging

Now lets merge our new_branch into main branch. If branches are not protected, you can use git merge command to merge branches. But first we will have to switch to main branch.

$ git switch main
$ git merge new_branch

Now our main branch is having changes from new_branch. Alt text (Ignore bat command in this image, it is just to print the content of README.md file.)

  • These new changes in main are only local, lets push to github.
$ git push origin main

Go on github to verify, switch to main branch and go to commits. Alt text Alt text Notice the commit we made in new_branch has appeared here. Meaning the changes made in new_branch are successfully merged inmain.

We can also manage branches on github by going to branches tab. Alt text Alt text Source here means which branch to copy. When we did it locally it automatically copied the branch we had head on.

Just like changes made locally has to be pushed in order to sync Local -> Github. We need to sync Github -> Local because we now have made changes on Github which are not in our local repo. To do this we have to use git pull command.

$ git pull

It will sync the changes from github to local repo. Alt text

If the branches are protected then you will have to create a pull request to merge the branches. This is generally considered as a good and safe practice. Let's do that next.

Creating a pull request

Here, I will quickly create a new branch and make some changes to it and push.

$ git branch new_branch2
$ git switch new_branch2
  • Make some changes to README.md file. Alt text
  • add, commit and push to github.
$ git add .
$ git commit -m "Update README in new_branch2"
$ git push origin head

Switch to new_branch2 on github (Refresh page if you aren't seeing new branch). Alt text Alt text It says "1 commit ahead of main" and there's an option to Contribute, click there. Alt text Click on "Open pull request". (You can Compare branches without creating PR) Now give a meaningful name to our PR. You can also write meaningful comment to describe the changes you have made. Alt text Before clicking "Create pull request" let's verify the changes we are going to make in the main branch. So scroll down on the same page and you will see the differece between the branches. Alt text Click on "Create pull request" and our PR will be made. You can click on Pull requests tab on your repo to check it. Currently it is open, meaning it is not merged or closed yet. Alt text Also, it says to no conflicts with base branch. We will see what it means soon. Now click on "Merge pull request" and then "Confirm merge". Alt text Now our PR is merged. We can verify our main branch now. The new_branch2 has been successfully merged into main. Alt text

Making your first open source contribution [Activity]

  1. Create a fork of https://github.com/gdsc-ghrietn

A GitHub fork is a copy of a repository that allows you to freely experiment with changes without affecting the original project.

  1. Clone the forked repository.
  2. Create a new branch.
  3. Make changes: In Contribute folder add a file with your name with .txt extension. Include your name and other details in the file.
  4. Add, commit, push changes to forked repo.
  5. Create a pull request.

BONUS

We will be showcased during the event briefly.

Resolving merge conflicts

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/addressing-merge-conflicts/resolving-a-merge-conflict-using-the-command-line

VS code integration with git

https://code.visualstudio.com/docs/sourcecontrol/overview

git-github's People

Contributors

sahilyeole avatar gdsc-ghrietn avatar vaibhavtalkhande avatar ayushsarode avatar

Stargazers

Sumit Karanjekar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.