0% found this document useful (0 votes)
1 views23 pages

31 Git-Github

Git is a widely-used version control system created by Linus Torvalds in 2005, enabling developers to track code changes, collaborate, and manage projects through repositories. GitHub serves as an online platform for hosting Git repositories, facilitating collaboration among developers. Key functionalities include staging, committing changes, branching, and merging, as well as pushing and pulling updates to and from remote repositories.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views23 pages

31 Git-Github

Git is a widely-used version control system created by Linus Torvalds in 2005, enabling developers to track code changes, collaborate, and manage projects through repositories. GitHub serves as an online platform for hosting Git repositories, facilitating collaboration among developers. Key functionalities include staging, committing changes, branching, and merging, as well as pushing and pulling updates to and from remote repositories.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

What is Git?

• Git is a popular version control system. It was created by Linus


Torvalds in 2005, and has been maintained by Junio Hamano since
then.
• It is used for:
• Tracking code changes
• Tracking who made changes
• Coding collaboration
What does Git do?
• Manage projects with Repositories
• Clone a project to work on a local copy
• Control and track changes with Staging and Committing
• Branch and Merge to allow for work on different parts and versions of
a project
• Pull the latest version of the project to a local copy
• Push local updates to the main project
Working with Git

• Initialize Git on a folder, making it a Repository


• Git now creates a hidden folder to keep track of changes in that folder
• When a file is changed, added or deleted, it is considered modified
• You select the modified files you want to Stage
• The Staged files are Committed, which prompts Git to store a permanent
snapshot of the files
• Git allows you to see the full history of every commit.
• You can revert back to any previous commit.
• Git does not store a separate copy of every file in every commit, but keeps
track of changes made in each commit!
Why Git?

• Over 70% of developers use Git!


• Developers can work together from anywhere in the world.
• Developers can see the full history of the project.
• Developers can revert to earlier versions of a project.
What is GitHub?
• GitHub is an online hosting service for Git repositories.
• GitHub lets you store your repo on their platform.
• Another awesome feature that comes with GitHub is
the ability to collaborate with other developers from
any location.
How to configure Git
• To verify this, you can run this command on the command line:
> git --version.
This shows you the current version installed on you PC.
• The next thing you'll need to do is to set your username and email
address. Git will use this information to identify who made specific
changes to files.
• To set your username, type and execute these commands:
> git config --global user.name "YOUR_USERNAME“
>git config --global user.email "YOUR_EMAIL“
Note: Use global to set the username and e-mail for every repository on your computer.
If you want to set the username/e-mail for just the current repo, you can remove global
How to Create and Initialize a Project in Git
• Now to initialize your project, simply run
> git init
• This will tell Git to get ready to start watching your files for every
change that occurs.
How to add files in Git

• When we first initialized our project, the file was not being tracked by
Git. To do that, we use this command git add . The period or dot that
comes after add means all the files that exist in the repository. If you
want to add a specific file, maybe one named about.txt, you use
>git add about.txt
$ git add .

Now our file is in the staged state. You will not get a response after
this command, but to know what state your file is in, you can run the
git status command.
Committed state and Modified state
• A file is in the committed state when all the changes made to the file
have been saved in the local repo. Files in the committed stage are
files ready to be pushed to the remote repo (on GitHub).

• A file in the modified state has some changes made to it but it's not
yet saved. This means that the state of the file has been altered from
its previous state in the committed state.
Staged state

• A file in the staged state means it is ready to be committed. In this


state, all necessary changes have been made so the next step is to
move the file to the commit state.
How to commit files in Git

• The next state for a file after the staged state is the committed state.
To commit our file, we use the
>git commit -m "first commit“
• The first part of the command git commit tells Git that all the files staged are
ready to be committed so it is time to take a snapshot.
• 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.
Push the repository to GitHub

• The first command


• >git remote add origin
https://github.com/ihechikara/git-and-github-tutorial.git
• creates a connection between your local repo and the remote repo on
Github.
• the project already exists locally so we will use commands in the "…or push
an existing repository from the command line" section. These are the
commands:
• git remote add origin
https://github.com/ihechikara/git-and-github-tutorial.git
• git branch -M main
• git push -u origin main
Cont….
• The first command git remote add origin
https://github.com/ihechikara/git-and-github-tutorial.git creates a connection
between your local repo and the remote repo on Github.
• The second command git branch -M main changes your main branch's name to
"main". The default branch might be created as "master", but "main" is the
standard name for this repo now.
• The last command > git push -u origin main
pushes your repo from your local device to GitHub. You should get a response
similar to this:
Cont…
• We first add the file by using
• git add . which adds all the files in the folder (one file in our case).
• Then we commit the file by running
• git commit -m "added new task" followed by
• git push -u origin main.
• Those are the three steps to pushing your modified files to GitHub.
How to Use Branches in Git
• With branches, you can create a copy of a file you would like to work
on without messing up the original copy. You can either merge these
changes to the original copy or just let the branch remain
independent.

• At this point, I want to add more tasks to the list but I am not yet sure
whether I want them on my main list. So I will create a new branch
called test to see what my list would look like with more tasks
included.
create a new branch
• To create a new branch, run this command:
• $ git checkout -b test .
• I will break it down. checkout tells Git it is supposed to switch to a new
branch. -b tells Git to create a new branch. test is the name of the branch
to be created and switched to.
• After committing your test branch, switch back to the main branch by
running this command:
• $git checkout main.
• You can check all the branches that exist in your repo by running the
• $git branch command.
• Now we can merge the changes we made in the test branch into the main branch by
running.
• $git merge test.
Push new branch to git
• If you would like to push your test branch, switch to the branch using
• $git checkout test and then run
• $ git push -u origin test
To view all branches in repository
• $git branch –a /-r
• -a represents all the braches from local and remote
• It display all the branches in remote repository.
How to Pull a Repository in Git
• To pull in Git means to clone a remote repository's current state into your
computer/repository.
• This comes in handy when you want to work on your repo from a different
computer or when you are contributing to an open source project online.
• Go to GitHub, and on your repository's main page you should see a green
button that says "Code".
• When you click on the button, you should see some options in a dropdown
menu. Go on and copy the HTTPS URL. After that, run
• $git clone YOUR_HTTPS_URL.
• This command pulls the remote repository into your local computer in a
folder

You might also like