Practical Guide To Using Git With Github
Practical Guide To Using Git With Github
TUTORIAL: GUIDE
USAGE PRACTICE
GIT WITH GITHUB
GIT GUIDE WITH GITHUB
What is version control?
The version control systems they are programs that aim to control the
changes in the development of any type of software, allowing to know the current state
of a project, the changes that have been made to any of its parts, the people
who intervened in them, etc. The version control software keeps track of
all modifications in the code in a special type of database. If a mistake is made
an error, developers can go back in time and compare previous versions
the code to help resolve the error while minimizing disruptions to
all team members.
Ultimately, having control over the changes in the codes of our application is a
crucial variable for the success of our development. Git it is a version control system
open source, designed to handle large and small projects quickly and
efficiency. The aim of this tutorial is to address the basic use of Git by providing
Useful practical examples to start managing remote repositories with platforms
like Bitbucket or GitHub .
1
The quality of open source software is easy to analyze and countless
companies depend greatly on that quality.
Git has a large user base and strong support from the community.
documentation is exceptional and by no means scarce, as it includes books, tutorials, and websites
specialized websites, as well as podcasts and video tutorials.
The fact that it is open source reduces the cost for amateur developers,
since they can use Git without having to pay any fee. As for
Open source projects, there is no doubt that Git is the successor to the previous ones.
generations of the successful open source version control systems, SVN and
CVS.
GITHUB
GitHub is a platform created to host the code of any applications.
developer. The platform is created for developers to upload the code of
its applications and tools, and as a user, you can not only download it
application, but also to enter their profile to read about it or collaborate in its development.
Git, being a control system, will be the tool that will allow us to compare the
code of a file restore versions
to see the differences between the versions
old if something goes wrong, and merge changes from different versions.
Thus, Github is a portal for managing projects using the Git system.
2
CREATE A REPOSITORY IN GITHUB
To upload your project to GitHub, you will need to create a repository to host it. In order to
to create a repository we will need to create an account on GitHub.
1) In the upper right corner of any page, use the dropdown menu and +
select New Repository
2) Write a short and easy-to-remember name for your repository. For example: 'hello-'
world
3) You can also add a description of your repository. For example, 'My first
repository on GitHub.
4) Choose the visibility of the repository. You can restrict who has access to a repository.
choosing the visibility of a repository: public or private. Public means that
anyone can see that repository and private means that only people
Authorized people can see it. Just because it is public does not mean that people can upload things.
to our repository, the only thing it allows is for the files to be viewed.
3
We can create the repository with a ReadMe
GIT INSTALLATION
Once we have created a repository on GitHub we will need to install Git
While this Git build is fine for some users, you may want to
install the most updated version. You can do it in many different ways; we have
I have compiled some of the easiest options below.
4
B. Follow the instructions to install Git.
Open a terminal and type the following text to check that the installation has been
successfully completed:
git_version
$ git --version
gitversion2.9.2
Homebrew installs a list of useful packages that do not come pre-installed on Mac.
B. The terminal will ask you to enter a password. Enter the password you use for
log in to your Mac and continue with the installation process.
C. Once finished, enter brew install git in the terminal and wait for it to download.
Check that Git is installed by running git --version .
2. Enter the following text to verify that the installation has been completed
correctly
git_version
$ git--version
gitversion2.9.2
5
4. Open the command prompt (or Git Bash if you selected not to use it during installation
Git from the Windows command prompt.
5. Enter the following text to verify that the installation has been completed
correctly
git_version
$ git --version
gitversion2.9.2
INITIAL CONFIGURATION
Open your Git terminal to start executing commands, for example, it will open
the program Git bash in Windows to access the command line of this program.
Once logged in, use the following command to set the git username:
Remember to replace the text in quotes with your real name. Now indicate the email
user's email for git:
Replacing the text in quotes with their email address. This setting
initial should be enough to get started. To check other values of your
current configuration execute:
The new configured values will be shown at the end, along with other configuration values.
predetermined:
...
auto
auto
...
Juan Perez
[email protected]
6
Let's choose option 1) for the moment, which will allow us to start from scratch and with the
we will be able to better appreciate what the basic operations with Git are. In this sense,
any operation you perform with Git must start with working locally,
so you need to start by creating the repository on your own machine. Even if your
The objectives are simply to upload that repository to Github so that other people can access it.
accessing through the remote hosting of repositories, you have to start working on
local.
In Windows, we can right-click on the folder and click on Git Bash Here , that gives us
It will open the Git Bash terminal in the folder to work with Git.
On a Mac we can do the same, right click on the folder and select the option New
Terminal in the folder .
Once stopped in our folder. To create a new repository, use the command git
init. git init is a command that is used only once during the initial setup of a
new repository. When you run this command, a new subdirectory will be created .git in your
current working directory. A new main branch will also be created.
$ git init
Git Status
The git status command gives us all the necessary information about the current branch.
git status
• If there are files in preparation (staged), not prepared (unstaged) or that are not there
receiving tracking (untracked)
7
Note: We will see the concept of branches later.
Git Add
When we create, modify, or delete a file, these changes happen locally and
They will not be included in the next commit (unless we change the configuration).
We need to use the git add command to include the changes from the file or files in your
next commit.
git add .
If you check the screenshot of the git status, you will see that there are filenames in red -
this means that the unprepared files. These files will not be included in your
commits until you add them.
We make a git add . to add our txt file. Once we do git add
we do another git status, now we will see that the files that were in red are in green
this means that we have already added them to make our commit.
Git Commit
This is perhaps the most used command in Git. Once you reach a certain point in the
development, we want to save our changes (maybe after a task or matter
specific) or upload a file / project.
8
We make a commit to save our txt and we put a message that explains what
we have done.
This command will take the alias of our repository and the URL of our repository in
GitHub will connect it with our local repository.
The alias we are going to use for Github is origin and to obtain the URL of our repository,
we can find it at the beginning of our repository:
We search for the URL of our GitHub repository and we do it in our terminal.
Anyway, if your branch was created recently, you may need to load and
push your branch with the following command:
Let's remember that we use the alias origin for the remote repository.
Once we have done this, if we refresh our repository we will see our
txt file in our GitHub repository.
9
This process will be repeated, removing the linking and the initialization of the repository, each
Once we make a change within our repository. This can be
modify an existing file or add more files to the local folder.
BRANCHES IN GIT
We often need to work with more than one person on the same project. But,
What happens if more than one developer makes changes to the same file? Or worse,
What happens if both change the same line of code?
To avoid this type of problems and permanently collide code, git provides the
branching tool (branch). This way, you can create your own branch of the
project and make all the changes you need, and at the end of the process create a pull
request to merge (combine your changes) with the main branch, main or master.
You can start your first practice to work with branches. We will do something as simple
how to run the command 'git branch' as it is. This will give us the list of branches that
let's have in a project. But it should be noted that the branches of a local repository
they can be different from the branches of a remote repository. For example, when you clone a
GitHub repository you are generally cloning only the master branch and not all
the branches that have been created over time. Another example is when you create a
branch in your local repository. In this case, you will simply have the branch in your project.
local and it will not be uploaded to the remote repository until you specify it.
You can see the branch you are on at any moment with the command:
git branch
This branch is the main one of your project and from which you can create new branches.
when you need it.
If you have made any commit in your repository, you will notice that after running the command
git branch informs us that the name of the branch is 'master'.
Let's remember that in GitHub this branch can be called Main, we can always change it.
branch name to Master with the GitHub configurations.
10
git checkout -b your_branch_name
If we look closely, we not only created a new local branch, but now we stand on the
new branch that we created.
We can obtain a more detailed description of the branches with this other command:
git show-branch
This shows us all the branches of the project with their completed commits. The output would be
like that in the following image.
As we can see, the new branch already has the first commit we made in the branch.
master because, as we explained above, we are cloning the master branch.
To switch between branches we use the command git checkout followed by the branch name
what we want to be the active.
This simple operation has a lot of power, because it will automatically change all of us.
the files of our project, those from all the folders, so that they have the content in
those who are in the corresponding branch.
At the moment in our example, both branches had exactly the same content.
but now we could start making changes to the ramaGit project and its
corresponding commits and then the files will have different codes, so that
you can see that when switching from one branch to another there are changes in the files.
As we explained before, each time you want to push a change to your branch, position yourself in
she executes the commands:
git add filename (dot (.) instead of the name if you want)
add all changed)
11
git commit -m "Message of the changes"
We are not going to make a push yet because we will explain that later, since
this will make our branch appear in the remote repository.
Having made a commit in our new branch, you will notice that when doing the show-
branches will show you new data:
If you are dedicated to editing your files, creating new files, and so on in the different branches
Then you will be able to observe that as you move from one to another, the project changes.
automatically in your editor, showing the current status in each of the branches where
you're getting situated. It's something fun, and if you're new to Git you'll see that it's a magic that works out.
quite surprising.
As you can see, the project can have several states at a given moment and you will be able to
move from one to another with total freedom and without having to change folders or anything like that.
The operation of publishing a branch remotely is done using the push command, indicating
the name of the branch you want to push. For example like this:
This is how we are doing a push, pushing to origin (which is the name usually given)
to the remote repository).
12
If you don't want to always use origin and the name of your branch in your pushes, you need to add it.
the previous push, before
-u the word origin. This will allow you to use git push only
and always go to that branch.
It is important to make sure that we do it on our own branch and not on master, since
We could send changes to the master branch thinking they were going to ours.
Once this is done, we could stop in our branch and simply write:
git push
You can upload as many commits as you see fit to your branch before merging to master.
It is always better to have small and frequent changes than few and large ones.
MERGE BRANCHES
As you create branches and change the state of folders or files, your project
It will start to diverge from one branch to another. The moment will come when you will be interested in merging.
branches to be able to incorporate the work done into the master branch.
13
The melting process is known as merge and it can be very simple or more
complex if there are changes that Git cannot process automatically. Git
to process the merges, use a common ancestor and check the changes that have been made
introduced to the project since then, combining the code from both branches.
To make a merge, we place ourselves on a branch, in this case the 'master', and we say with
which other branch should the code be merged into.
The following command, executed from the 'master' branch, allows merging it with the branch
ramaGit
Unmerge needs a message, just like happens with commits, so when doing that
the command will open 'Vim' (or any other console editor you have configured) to
You can exit Vim by pressing
the ESC key and then typing :q and pressing enter to accept that command. This
the operation to indicate the message can be summarized with the command:
Then we can verify that our master branch has all the new code from the
ramaGit and we will be able to make new commits in master to continue the development of our
project already with the main branch, if it is our wish.
PULL REQUEST
Previously, we had merged our branches through the command merge , but GitHub
allows us to merge our branches and also see the changes that are between one branch and
another, thanks to the Pull Request
We are going to stop again at a stage where we have not merged the branches. Until
at that point we had managed to separate our changes from the rest of the team,
but the time to publish our changes was approaching and the need to know and/or
validate how different our version is and to see that everything is fine to merge those changes.
This is where the pull request tool comes to the rescue.
To create a pull request we must go to the branches section, find our branch and
click on the button New pull request .
14
Before making our pull request, we can see how many commits (changes) there are.
they separate to another branch from master. If we look, we get two zeros, but if master were
one commit ahead of some branch would come out as a one in the zero on the left and so on
increase the number according to the number of commits ahead of master. Now, if
the number is to the right, it would mean that the other branch is x commits ahead of master.
15
It will show us something similar to the following.
References:
1. On the left, select the target branch that we want to merge into.
changes, our current branch to the right. We can always create a pull request
and change the branches we want to merge.
2. In this section we can put an informative title about what our
changes and a description as additional documentation. Details of the changes
proposed, test plan, etc.
3. This section shows us a summary of the commits and modified files.
4. In the last section, it shows us the details of the modified files. To
to visualize the changes we can switch between the 'Unified' and 'Split' view modes
Until this moment we have not created anything, we are just looking at a previous summary.
to continue we click on the 'Create pull request' button. Next, we will see the pull
request created in the following way.
On the other hand, we can use the 'Files changed' tab to do code review.
16
If we detect any line of code that requires changes, you can click on it and
add a comment so that the author of the pull request can modify it. It is not necessary
create a new pull request to update the changes, simply by making a
A commit on the branch is enough, GitHub takes the changes and updates the pull request.
automatically.
If everything is fine and there are no conflicts, we can merge our branch into master by clicking
on the “Merge pull request” button and in this way, the branch cycle ends.
Finally, we have the option to approve the changes so that the author of the pull request
merge on your change.
Once we merge the changes in our branches, it will say that the branch has been
merged.
17
DELETE A BRANCH
Sometimes it may be necessary to delete a branch from the repository, for example because
we may have made a mistake in the name when creating it. Here the operation may be different,
depending on whether we have already pushed that branch to remote or if it is still only local.
However, this action may not work for us because we may have made changes that
are not saved in the remote repository, or have not been merged with other branches. In
the case that we want to force the deletion of the branch, to delete it regardless of
if the push or merge has been done, you will need to use the option -D .
You must pay special attention to this option "-D", since deleting this way can
there are changes that can no longer be recovered. As you can see, it is quite easy to
confuse with "-d", a safer option, as it does not allow deletion of branches in situations
where code can be lost.
The process to obtain a branch from the remote repository is quite simple. First, we would use
the git checkout command to create the missing branch locally and we use -b to
to stand on it.
18
Once we have done that, we can get everything that is in the branch with the command
pull, putting the alias of the remote repository and the name of the branch:
GIT PULL
We just saw that we use the git pull command to download the branch, so let's go to
explain a little more about this command.
Git pull It is a Git command used to update the local version of a repository.
from another remote.
It is one of the four commands that requests network interaction by Git. By default, git
pull does two things.
Update the current working branch (the branch that has been switched to currently)
git pull retrieves (git fetch) the new commits and merges (git merge) them into your branch
local.
However, there are some things to keep in mind for that example to be
true
• If there are multiple remotes, git pull might not be sufficient information. It is possible
you should enter git pull origin or git pull upstream.
CLONE A REPOSITORY
Now we are going to talk about the cloning operation of a repository, the process you have
What to do when you want to bring the code of a project that is published on GitHub and
Do you want to restore it on your computer, so you can use it locally, modify it, etc.
This step is quite basic and very easy to do, but it is essential because you will need it.
do many times in your work as a developer. We will also try
complement it with some useful information, so that you can learn useful things and
a little more advanced.
DOWNLOAD VS CLONE
At the beginning of using a site like GitHub, if we have no idea how to use Git, we can also
obtain the code from a repository by downloading a simple Zip. You can get this option
through the button in the following image.
19
However, downloading a repository like this, although very simple, does not allow you some of the
interesting utilities of cloning it, such as:
• Does not create a local Git repository with the changes that the remote repository has.
held over time. That is to say, you download the code, but nothing more.
• You will not be able to push changes to the remote repository once you have made them.
locally.
In summary, you will not be able to generally use the advantages of Git in the downloaded code. So
it is better to clone, as learning to perform this step is also very simple.
First, you will copy the URL of the remote repository that you want to clone (see the 'Copy to' icon)
clipboard in the following image.
Then you will open a terminal window to navigate to your project folder that
you want to clone. I would recommend you to create a folder directly with the name of the
project that you are cloning, or any other name that seems better to you for this
repository. You locate yourself within that folder and from it we launch the command to do
the clone, which would be something like this:
git clonehttps://github.com/EggEducacion/MyFirstRepository.git.
20
The last point, after the URL copied from git, indicates that the clone will be placed in
the folder where you are located, in your terminal window. The output of that command would be
more or less like you have in the following image:
In this way, we already have the remote repository to work locally and we will be able to
make the changes we want and upload the changes with the commands we explained
previously.
21
5. Click on Invite a collaborator.
In the search field, start typing the name of the person you want.
invite, then click on a name from the list of results.
8. The user will receive an email inviting them to the repository. Once that
accept the invitation, you will have collaborator access to your repository. The invitations
licenses will expire after 7 days. This will reset any licenses without
complain.
22
LEARNING QUESTIONS
What is Git?
What is GitHub?
a) Git clone
b) Git commit
c) Git init
d) Git status
a) Git help
b) Git add
c) Git info
d) Git status
a) Git add
b) Git clone
c) Git status
d) Git init
a) Git clone
b) Git save
c) Git commit
d) Git add
a) Git send
b) Git push
c) Git pull
d) Git remote
23
What command do we use to create a branch?
a) Git branch
b) Git checkout -b
c) Git clone
d) Git init
a) Git merge
b) Git join
c) Git pull
d) Git push
10) To merge our changes from two branches in the remote repository we will use:
a) Pull Request
b) Git Merge remote
c) Pull Merge
d) None of the above
a) Git pull
b) Git clone
c) Git download
d) Git remote
24
LEARNING EXERCISES
Now it's time to put into practice everything seen in the guide.
WATCH VIDEOS:
1. We are going to create a folder with a txt file, inside put the text we want, this
the folder along with the txt must start as a local repository. Furthermore
We should upload this file to a remote repository.
2. Now we need to modify the txt and upload those changes to the remote repository.
WATCH VIDEOS:
3. For the following exercise, you will have to work in a team with your classmates.
table.
a) The facilitator of each team must create a public repository with the name
practice_github selecting the option Initialize this repository with a README.
b) Once the repository is created, the facilitator must invite the members of their group.
to the same. Click on the Invite a collaborator button and search for the members of your
table by username or email.
c) Each member must accept the invitation to the repository. Check the invitation.
email and click on View Invitation.
d) Clone the repository. Each team member must clone the repository with the
ReadMe file. After accepting the invitation, GitHub redirects you to the repository.
e) Each member of the table, including the facilitator, must create their own branch to
work on the ReadMe file
f) Now each member of the table must include their name in the ReadMe file.
local way and upload it to your branch.
g) When all members of the table have added their name to the ReadMe file,
one by one, merge all your changes into the master branch. In the end, they
There should be a ReadMe file with all their names.
4. Now you are going to continue working as a table. Your task now is that each member
From the table, including the facilitator, must create their branch and create one of the following
Cat, Dog, Horse, Rabbit, Bird, and Duck. Each one will assign the attributes
that you desire.
25
The facilitator will have to create the repository and upload an empty Java project for
that the members of the board can clone and create their class. Once each member
They have created their class in their respective branch, they must merge all the classes in the branch.
master, so that the final project is completed.
26