GitHub tutorial for beginners - LofNaDI/Tutorials GitHub Wiki
GitHub is a web-based interface that uses Git, an open-source version control software that lets multiple people make separate changes to web pages or any project at the same time. With GitHub, developers can build code, track changes, and innovate solutions to problems that might arise during the project development process simultaneously.
Using GitHub can be very helpful because you can recover all the versions of your project that you have been storing and/or check all the modifications that you have been recording during the development of your project, and much more.
Saving your project with GitHub you’ll store your projects in a web browser as a security-safe copy (“back up”) which you’ll have access to it from anywhere and anytime. So, you’ll never lose the whole project.
-
The first step is to create your GitHub account. Do it by going to GitHub web and signing up. Your user account is your identity on GitHub and your user can be a member of any organization.
-
Verify your email address after signing up for a new account, or when you add a new email address to your account (required). You won’t be able to do anything without it.
i. In the upper-right corner of any page, click your profile photo, then click Settings (above sing out).
ii. In the left sidebar, click Emails. Then, in "Add email address", type your email address and click Add.
iii. Verify your email address. Under your email address, click Resend verification email.
iv. GitHub will send you an email with a link in it. After you click that link, you'll be taken to your GitHub dashboard and see a confirmation banner.
-
Configure two-factor authentication (highly recommended). Get more info about it here
-
Create and add an SSH Key.
i. To create it, open Git Bash and follow the example.
ii. Finally to add it, go to your GitHub account and open the settings section. Click on SSH and GPG keys, then follow the next steps.
Before using GitHub, you’ll need to download, install, and configure Git.
-
Download and install the latest version of Git. Follow the web instructions depending on the OS that you are using on your local computer.
-
Set your username in Git. This part is equal for Mac, Windows, and Linux
i. Open Git Bash console.
ii. Set a Git username:
git config user.name _username_
orgit config --global user.name _username_
(e.g.git config user.name Mery)
. The first option is to set the user name for a single repository and the second option is to set the user name for each repository on your computer.iii. Check that is done using the following command:
git config user.name
orgit config --list
.
You'll have to use the same commands on the Git Bash console to configure your email and your password/token (Windows requirement): git config --global user.email "[email protected]"
. Before doing that you should complete the next point.
-
Set your commit email address in Git. This part is equal for Mac, Windows, and Linux
i. In the upper-right corner of any page, click your profile photo, then click Settings (above sing out).
ii. In the left sidebar, click Emails. Then, in "Add email address", type your email address and click Add.
iii. Check that is done using the following command:
git config user.name
orgit config --list
.
- Repository (“repo”): a folder in which all files of a project and their versions histories are stored
- Branch: a workspace in which you can make changes that WON’T affect the live site.
- Commit changes: a saved record of a change made to a file within the repo
- Pull request: the way to ask for changes made to a branch to be merged into another branch that also allows for multiple users to see, discuss and review work being done.
- Merge: After a pull request is approved, the commit will be pulled in from one branch to another and, deployed on the live site.
- Issues: how work is tracked when using git. Issues allow users to report new tasks and content fixes, as well as allows users to track progress on a project board from beginning to end of a specific project.
To store your projects on GitHub, you’ll need to create a repo for them to live. You can create public (open-source projects) or private repositories. If you create a public repository, you should include a license file that determines how you want your project to be shared with the GitHub community.
There are different ways to create a repo. In this section, you’ll learn how to do it by using a web browser:
- Sing in and then on your user main page (e.g. github.com/username), you have to click on "repositories".
- It’ll send you to the repository's main page where you could find all the repositories that you’ll create. From here, you should click on the green bottom “New”.
- From this window, you could set the configuration of your new repository. Type a short name (e.g.'myfirstrepo') and choose repository visibility. Also, you can add a tiny description, if you want, or you can do it later on. The last step is to set how you want to initialize this repository. You can do it by adding some of the given suggestions. These suggestions are optional and you could add them when you need them.
- Before starting using the command line, you should copy the remote repository SSH or HTTP. It will appear after you create the repository.
-
Now, let's link the remote repository (e.g. 'myfirstrepo') with your local machine. First, pick a working directory (e.g. Git_repo folder) and open the Git Bash console to start using the command lines.
-
Once you have opened the console, you'll need to create an empty local repository as the following example. Use the same repo name that you called it to create the remote repository.
- Then, move into the local repository and create a file to store in it (e.g README.md). After that, you'll can **add this change to your remote repository following the command lines as in the example. First add changes to the staging area and commit changes.
- Before pushing the commit for the first time, you'll need to set the proper branch ('main') and configure the link between your local and remote repo. To do this you'll paste the repo SSH or URL (that you copied after creating the repo) using the command as in the example. Once you have done that you'll be ready to push the first commit and be done.
-
Once, you have created a repo it´s recommended to create a branch on that. By doing this, you'll create a workspace where you could modify your project and commit changes, on this branch, without affecting the default branch. Your collaborators will take the chance to review your work before committing changes on the default branch. To create a branch, navigate to the repository's main page. Click on the branch button, type a new branch name, and finally, select create a branch.
-
Pull request is an action to ask collaborators for feedback on your changes. When you create a pull request you should include a summary of the changes and what problem they solve. Also if your pull request addresses an issue, link the issue. More information about this topic here.
-
Address review comments to let the reviewers leave questions or suggestions. They could do that on the whole pull request or to specific lines. This will allow you to continue committing and pushing changes in response.
-
When you are done and your pull request approved you could merge all the pushed modifications so that your changes will appear on the default branch. Consider that by setting branch protection you'll specify requirements which branch has to meet before merging.
-
Once you have merged your pull request then you could delete your branch because the objective was completed.
When you do a fork that means you'll be doing a copy of a repository in which you are interested. Forking a repo allows you to experiment without affecting the original project. Commonly, they are used to propose changes to someone's project or to use someonés project as an initial point for your own project/.idea
- Choose a repo to fork and go to the repo home page.
- Look for the fork button (on the upper-right corner) and press there.
- Select where you'll want to paste that repo (your user or organization account)
- Wait until the end of the process and check that you'll be done.
git --help -a
or git -h -a
>> Displays the basic command lines with small descriptions.
git help <command>
>> To read more about a specific command.
git clone 'URL/SSH'
>> Clone a repository into a new directory (local machine).
git pull
>> To fetch and integrate the latest changes from the remote repository to the local repository.
git pull origin master
>> Pull down from the remote repository to make sure you have up-to-date information.
git checkout
>> Switch branches or restore working tree files.
git checkout -b 'new_branch_name'
>> Create your own branch (if you are working on a group project)
git status
>> Shows the working tree status. Run it before you use git add to confirm that your modified files have been added to the staging area. It will appear in green color if they are added. Otherwise, it will appear in red color and you should try to add them again.
git add 'file_name'
>> To add file contents from the working directory to the staging area or the working tree.
git add *
>> To add all the local repo changes from the working directory to the working tree.
git commit -m "comment"
>> Record the local repo changes to the repository
git push
>> To transfer your changes from the local repo to the remote repo, overwriting the master branch.
git push 'branch_name'
>> To transfer your changes from the local repo to the remote repo. Specifying the branch name only writes the changes made on that branch, without affecting the master branch. It´s a good way to keep safe the original project from possible mistakes.