Beginner's Guide to Git and Github 11-18-2018, 09:25 PM
#1
Introduction
When it comes to source code, Github is a major player. It's almost essential these days to know how to use Git. Repositories (also called "repos") are basically a place to store code online for everyone to see. You can use Github for free, but they also have premium accounts that allow you to have private repos. Github is a very important website for those of us who love open source and free software. You can find everything there, even rats and crypters that the kiddos love so much, and even botnets, ransomware, worms, viruses, you name it.
Using Github also helps to show people that your software isn't doing anything it shouldn't be doing. Using something like Github also allows other programmers to contribute to your projects and may even help other people learn from your code. Using Github to host your projects will also eliminate you from having to use free file sharing sites that are a waste of time and space on the internet. Nobody wants to wait 60 seconds to download your file.
Downloading a Repo
Downloading a repo to your machine is pretty simple, it's called "cloning".
Creating a new Repo
To create a new repo, move into the directory you wish to create the repo in. You can start with a fresh empty directory or you can use a directory your project is currently in. Move into the directory you want to create the new repo and type git init. I have create a directory named "newdir" in my web root (/var/www/html/newdir). Typing git init will initialize a new Git repo in this directory. When you type the command you should see a message like the one below:
In the newly created .git directory is the configuration and other information about the repo, you can see the directory structure below:
You can create the Git repo on Github before OR after, it doesn't matter. To create a new repo visit https://github.com/new and you will be presented with a page like the one in the image below:
![[Image: LugC5y2.png]](https://i.imgur.com/LugC5y2.png)
In the image above I have already filled in the name for my repo, newdir, since that is directory I created for this tutorial. Yes, when I am done with this tutorial you can visit the repo online. You can also name your repo newdir if you would like, it won't hurt anything. However, when naming your repository you might want a more unique name to avoid naming conflicts in the future.
Once you click the green create button you will be redirected to the new repo. On this page it will tell you what to do, but for redundancy sakes, I will also tell you what to do. It's pretty easy, first you need a file to push into the new repo, which is why it tells you to create the README.md file. You don't need the README file but it sure helps when other people are viewing your repo.
Authenticating Yourself in the Config
Once you are ready to start sending files, you need to tell git who you are. You will be using your git information in the config. Git, of course, has handy commands to do this. Below is a command to identify yourself, replace you@example.com with your email. You can also set the default username and email simply by removing the --global argument.
and next, supply git with your username on Github:
The next thing you need to do is tell git where this repository is on Github, for me, my Github link for this is, https://github.com/therealsunjester/newdir.git. This is the link we are adding to the remotes in the config, again, there is a simple command for this also (replace my link with yours of course):
You can add my link to your remotes but unless you know my Github login details it will fail to push anything to my repository. If you supply the wrong details (username/password), the push will also fail to your repository.
Committing Files
Once you have some files in the directory, you need to add them to the commit. There is a simple command for adding all the files in your new repo folder:
Yes, there is a period at the end of that command, it means "current directory" (it's a common UNIX directory thing, hopefully you already knew that).
Next, you have to make a commit. A commit is basically like telling git that you have files that are ready to be pushed live to the repo, you can think of this as "staging". Once a file is staged, if you modify it, you will need to recommit it. Below shows you how to commit your first files to the repo. When committing you will need to supply a message, that's done with the -m flag. You can see below that the command I used was, git commit -m "my first commit"
Pushing Changes to the Repo
If you have created a new repo and the repo doesn't exist on Github yet you will get an error like the one below. If you have been following along, you will not receive this error. If you do, don't worry, you just need to add the remote to the config, you can scroll up to the "Authenticating" part of this tutorial to find out how.
Once you have added some files and committed the files you are ready to push them to the master branch. Hopefully you will figure out how to use branches and I won't have to write another tutorial for Git, but then again, maybe I will write one anyway. Next, issue the push command, as shown below. Git will ask you for your username and password, then push the changes you have in your commit.
Now your first repository should be on Github, you can view the repository I made by visiting this link: https://github.com/therealsunjester/newdir
If you would like a more in-depth and more advanced tutorial, let me know and I will cover pulls, branches and other fun stuff you can do with Github. If I left something out that you think should be added, reply to this thread or hit me up on my email.
When it comes to source code, Github is a major player. It's almost essential these days to know how to use Git. Repositories (also called "repos") are basically a place to store code online for everyone to see. You can use Github for free, but they also have premium accounts that allow you to have private repos. Github is a very important website for those of us who love open source and free software. You can find everything there, even rats and crypters that the kiddos love so much, and even botnets, ransomware, worms, viruses, you name it.
Using Github also helps to show people that your software isn't doing anything it shouldn't be doing. Using something like Github also allows other programmers to contribute to your projects and may even help other people learn from your code. Using Github to host your projects will also eliminate you from having to use free file sharing sites that are a waste of time and space on the internet. Nobody wants to wait 60 seconds to download your file.
Downloading a Repo
Downloading a repo to your machine is pretty simple, it's called "cloning".
Code:
git clone http://github.com/repo-name-hereCreating a new Repo
To create a new repo, move into the directory you wish to create the repo in. You can start with a fresh empty directory or you can use a directory your project is currently in. Move into the directory you want to create the new repo and type git init. I have create a directory named "newdir" in my web root (/var/www/html/newdir). Typing git init will initialize a new Git repo in this directory. When you type the command you should see a message like the one below:
Code:
Initialized empty Git repository in /var/www/html/newdir/.git/In the newly created .git directory is the configuration and other information about the repo, you can see the directory structure below:
Code:
(xenial)sunjester@localhost:/var/www/html/newdir/.git$ ls -l
total 32
drwxrwxr-x. 2 sunjester sunjester 4096 Nov 18 05:19 branches
-rw-rw-r--. 1 sunjester sunjester  92 Nov 18 05:19 config
-rw-rw-r--. 1 sunjester sunjester  73 Nov 18 05:19 description
-rw-rw-r--. 1 sunjester sunjester  23 Nov 18 05:19 HEAD
drwxrwxr-x. 2 sunjester sunjester 4096 Nov 18 05:19 hooks
drwxrwxr-x. 2 sunjester sunjester 4096 Nov 18 05:19 info
drwxrwxr-x. 4 sunjester sunjester 4096 Nov 18 05:19 objects
drwxrwxr-x. 4 sunjester sunjester 4096 Nov 18 05:19 refsYou can create the Git repo on Github before OR after, it doesn't matter. To create a new repo visit https://github.com/new and you will be presented with a page like the one in the image below:
![[Image: LugC5y2.png]](https://i.imgur.com/LugC5y2.png)
In the image above I have already filled in the name for my repo, newdir, since that is directory I created for this tutorial. Yes, when I am done with this tutorial you can visit the repo online. You can also name your repo newdir if you would like, it won't hurt anything. However, when naming your repository you might want a more unique name to avoid naming conflicts in the future.
Once you click the green create button you will be redirected to the new repo. On this page it will tell you what to do, but for redundancy sakes, I will also tell you what to do. It's pretty easy, first you need a file to push into the new repo, which is why it tells you to create the README.md file. You don't need the README file but it sure helps when other people are viewing your repo.
Authenticating Yourself in the Config
Once you are ready to start sending files, you need to tell git who you are. You will be using your git information in the config. Git, of course, has handy commands to do this. Below is a command to identify yourself, replace you@example.com with your email. You can also set the default username and email simply by removing the --global argument.
Code:
git config --global user.email "you@example.com"and next, supply git with your username on Github:
Code:
git config --global user.name therealsunjesterThe next thing you need to do is tell git where this repository is on Github, for me, my Github link for this is, https://github.com/therealsunjester/newdir.git. This is the link we are adding to the remotes in the config, again, there is a simple command for this also (replace my link with yours of course):
Code:
git remote add origin https://github.com/therealsunjester/newdir.gitYou can add my link to your remotes but unless you know my Github login details it will fail to push anything to my repository. If you supply the wrong details (username/password), the push will also fail to your repository.
Committing Files
Once you have some files in the directory, you need to add them to the commit. There is a simple command for adding all the files in your new repo folder:
Code:
git add .Yes, there is a period at the end of that command, it means "current directory" (it's a common UNIX directory thing, hopefully you already knew that).
Next, you have to make a commit. A commit is basically like telling git that you have files that are ready to be pushed live to the repo, you can think of this as "staging". Once a file is staged, if you modify it, you will need to recommit it. Below shows you how to commit your first files to the repo. When committing you will need to supply a message, that's done with the -m flag. You can see below that the command I used was, git commit -m "my first commit"
Code:
(xenial)sunjester@localhost:/var/www/html/newdir$ git commit -m "my first commit"
[master (root-commit) aada346] my first commit
1 file changed, 1 insertion(+)
create mode 100644 README.mdPushing Changes to the Repo
If you have created a new repo and the repo doesn't exist on Github yet you will get an error like the one below. If you have been following along, you will not receive this error. If you do, don't worry, you just need to add the remote to the config, you can scroll up to the "Authenticating" part of this tutorial to find out how.
Code:
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using
  git remote add <name> <url>
and then push using the remote name
  git push <name>Once you have added some files and committed the files you are ready to push them to the master branch. Hopefully you will figure out how to use branches and I won't have to write another tutorial for Git, but then again, maybe I will write one anyway. Next, issue the push command, as shown below. Git will ask you for your username and password, then push the changes you have in your commit.
Code:
(xenial)sunjester@localhost:/var/www/html/newdir$ git push -u origin master
Username for 'https://github.com': therealsunjester
Password for 'https://therealsunjester@github.com':
Counting objects: 3, done.
Writing objects: 100% (3/3), 237 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote: Â Â Â https://github.com/therealsunjester/newdir/pull/new/master
remote:
To https://github.com/therealsunjester/newdir.git
* [new branch] Â Â Â master -> master
Branch master set up to track remote branch master from origin.Now your first repository should be on Github, you can view the repository I made by visiting this link: https://github.com/therealsunjester/newdir
If you would like a more in-depth and more advanced tutorial, let me know and I will cover pulls, branches and other fun stuff you can do with Github. If I left something out that you think should be added, reply to this thread or hit me up on my email.
(This post was last modified: 12-02-2018, 05:48 PM by sunjester.)


![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)
















