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/
- Setting up git and github
- Basic terminal commands for navigation
- What is version control system?
- Creating & cloning a repository
- Making changes
- Pushing changes to github
- Git branches and merging
- Creating a pull request
- Making your first open source contribution [Activity]
-
Creating github account: https://github.com/signup
-
Downloading git: https://git-scm.com/downloads
-
Installing git (guide): https://phoenixnap.com/kb/how-to-install-git-windows
Note: In that guide, ignore
How to Launch Git in Windows
and later. -
Make sure
git -v
command works in your terminal. If it doesn't, there might be some problem with the installation or in setting up the path. Try reinstalling git and make sure to follow the guide properly. -
Install github cli for login. For authenticating our git cli. https://www.techielass.com/install-github-cli-on-windows/ (Install Chocolatey from here if needed: https://chocolatey.org/install) Executing
gh auth login
command.
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.
-
pwd
(Print Working Directory): Displays the current directory path.Example:
$ pwd /home/user/documents
-
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 ../..
- To move forward (into a subdirectory):
-
ls
(List): Lists the files and directories in the current directory.Example:
$ ls file1.txt file2.txt directory1 directory2
- 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.
- The types of VCS are:
- Local Version Control System
- Centralized Version Control System
- Distributed Version Control System
- Git is the most well-known example of distributed version control systems.
- Open your github profile and go to
Repositories
tab. - Click on
New
button. - Give a name to your repositor.
- Make sure to check
Add a README file
option. - Click on
Create repository
button.
- After creating a repository you will be redirected to the repository page. Or else you can access it from Repositories tab in your profile.
- Click on
Code
button. - Copy the https link of your repository.
- Open your bash terminal and
cd
into your desired location (for me Developer folder).
$ cd Developer
- Open your bash terminal and enter
git clone [your repo link]
git clone https://github.com/sahilyeole/Test.git
- 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.
cd
into that folder in terminal.
$ cd Test
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.
- 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
- Add some text to the file and save it.
Hello world
- Now go to your terminal and type
git status
command.
$ git status
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.
- 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
- Enter
git status
command again to see the status of the repository.
$ git status
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
- 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.
- Try
git status
command again.
$ git status
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.
- 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.
- After 5th step your commits are pushed to github. You can verify this by going to your repository page on github.
Git branches are parallel versions of a codebase that allow developers to work on different features or fixes without impacting the main code.
git branch
to list all the available branches.
$ git branch
Currently there's only one branch named main
.
Press q to get out.
git branch [branch name]
to create a new branch.
$ git branch new_branch
now run git branch
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
Also, notice prompt in our terminal is showing new_branch
.
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.
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. This new branch is having the changes we have made in this branch.
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
.
(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.
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. 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.
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.
Here, I will quickly create a new branch and make some changes to it and push.
$ git branch new_branch2
$ git switch new_branch2
$ 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).
It says "1 commit ahead of main" and there's an option to Contribute, click there.
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.
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.
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.
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".
Now our PR is merged.
We can verify our main
branch now.
The new_branch2
has been successfully merged into main
.
- 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.
- Clone the forked repository.
- Create a new branch.
- Make changes: In Contribute folder add a file with your name with .txt extension. Include your name and other details in the file.
- Add, commit, push changes to forked repo.
- Create a pull request.
We will be showcased during the event briefly.