Login Register


Git and GitHub for developing and sharing code filter_list
Author
Message
Git and GitHub for developing and sharing code #1
Hello [username], I'm new to this site but from what I've seen there are a lot of very helpful people here so I felt why not do the same.

Why I'm Writing This
Lots of people here program and lots of people here share what they create. Unfortunately the way they share it is via Pastebin, random site download, etc which is bad for a few reasons...
  • It's shady as all hell (Sweet zip file bro)
  • It's easy to loose changes (rm -rf *)
  • It's hard to collaborate (Wait, you made changes?)
  • Etc

Using a tool like Git allows you to fix all of those things. You can monitor changes, you can fork things out into branches (I'll explain), you can easily and safely share plain text code.

What is Git
"Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency." -- git-scm.com

What is GitHub
"GitHub is a Git repository web-based hosting service which offers all of the functionality of Git as well as adding many of its own features. Unlike Git which is strictly a command-line tool, Github provides a web-based graphical interface and desktop as well as mobile integration." -- Wikipedia


Git Basics
Now that we have explained all of that let's jump right in...

I'm not going to cover installation of Git but you can read it here How to install git or if you're on Ubuntu..
Code:
~$ sudo apt-get install git

Language
  • Repository (Repo) - Essentially your whole project including branches, remotes, etc.
  • Branch - A version of your current repo common branch names would include Master, Dev, Staging, Upgrade, and DontTouchThisBranchItBreaksShit
  • Remote - This is a remote git repo that your local repo can exchange updates and branches (Like your github account)
  • Origin - The remote that your repo originated from (applies to cloned repos only)
  • Checkout (Branches) - Change the active branch
  • Checkout (File) - Remove any changes since the last commit
  • Commit(s) - This is how you submit changes to a branch
  • Log - The log of all the branch's commits
  • Push - The act of push code from one repo to another (or to a remote)
  • Pull - Requesting all changes from a specific repo's branch
  • Merge - Merging two local branch's code changes (Most common case is merging dev changes into master)


How to create a repository
There are two methods of creating a repository, git init or git clone.

If starting a new project from scratch you need to use git init. If you are downloading/cloning a project you would use git clone.

Example:
This is an example of cloning a repo from GitHub, after the commands we have the full code base for the lulzlabs "AirChat" program.

Code:
~$ git clone git@github.com:lulzlabs/AirChat.git

This is an example of creating a file and then building a git repo around that file. After this we have a git repo around a single file called "file.php" with the contents "echo 'test';".
Code:
~$ mkdir coolscript && cd coolscript ~$ touch file.php ~$ echo "echo 'test';" > file.php ~$ git init ~$ git add file.php ~$ git commit -am "Creating a new git repo for my hax0r project"


Adding a new file
To add a new file to a git repo you must first have a repo and a file(s) to add. After that is confirmed you can use the command "git add" to add those files. This must be followed by a commit.

Example:
This example shows creating a new empty file and then adding it to the git repo.
Code:
~$ touch newfile ~$ git add newfile ~$ git commit -am "Adding a new file to the repo"

Committing Changes to Branch (Editing Files)
Whether you are editing a file, adding a new file, or changing permissions(if you git config says to record that) you must commit your changes before you can push your branch to another repo. The best way to see if you have changes would be to run the command "git status" this will show files that aren't in the git repo as well as any edits.

With every commit you need a commit message this will allow you to keep track of everything. I do that by adding -m "MY MESSAGE HERE" in my git commit call. I also like to to use -a so I don't have to manually confirm my changes.

Example:
In this example we edit "existingfile" and commit our change.
Code:
~$ echo "Add to file" > existingfile ~$ git status #this is optional ~$ git commit -am "Editing existingfile to have new message."

In this example we edit "existingfile" and commit our change (Without the -a in our git commit).
Code:
~$ echo "Add to file" > existingfile ~$ git status #this is optional ~$ git add existingfile ~$ git commit -m "Editing existingfile to have new message."


Reverting Changes
Mistakes happen and that's why we have checkout. Using the command "git checkout FILENAMEHERE" will allow you to revert that file to the last git commit.

Example:
This shows editing a file, a commit, then an incorrect edit, and finally reverting that file. I use "cat" to show the file changes.

Code:
~$ echo "Edit to file" > existingfile ~$ git commit -am "Changing file" ~$ echo "wrong edit to file" > existingfile ~$ cat existingfile #this will output "wrong edit to file" ~$ git checkout existingfile ~$ cat existingfile #this will output "Edit to file"

Branches
A branch is an individual line of development(like a fork) that allows you to keep versions of your code separate. This is useful mainly for keeping stable versions and development versions segregated. I personally use ticket systems for big projects so I can have a branch per ticket. So I would create a branch, do my edits, commit, test, and merge into master.

Example:
In this example we create a dev branch and check it out (Make dev the active branch). NOTE: the * shows the currently checkout branch.
Code:
~$ git branch * master ~$ git branch dev ~$ git branch dev * master ~$ git checkout dev Switched to branch 'dev'

Delete Branches
To delete a branch you can do one of two things. The safe delete and unsafe delete. Safe delete exits if you have unmerged chages unsafe does it with or without checking for unmerged changes. To safe delete "git branch -d BRANCHNAME" to unsafe delete "git branch -D BRANCHNAME"

Code:
~$ git branch newbranch ~$ git branch -D newbranch


Git Remotes
As explained in the language section a git remote is a remote git repo that you have access to. But git remotes can be a monster of their own.

View Remotes
This command lets you list all remotes you have added to your repo.
Code:
~$ git remote

Add A Remote
To add a remote you use the following..
Code:
~$ git remote add <name> <url>

An example could be (You can use both http and ssh urls)
Code:
~$ git remote add origin ssh://user@host/path/to/repo.git

Remove a Remote
Doesn't really need explaining
Code:
~$ git remote rm <name>

Fetching Branches from a repo
It you want a specific branch from a remote repo you want to fetch.
Code:
~$ git fetch <remote> <branch>

Example:
Code:
~$ git fetch origin someRandomBranch

If you want ALL branches from a repo just exclude the branch name

Example:
Code:
~$ git fetch origin

Pulling Changes From Repo
Before pushing to a branch you want to make sure your local branch is up to date OR if you just want to update your local branch you would...
Code:
~$ git pull <remote> <branch>

Example:
Code:
~$ git pull origin dev


Pushing Changes From Repo
If you want to push your commits(changes) to a remote repo's branch you would use git push.

Code:
~$ git push <remote> <branch>

Example:
Code:
~$ git push origin dev


Using GitHub With Git
Before playing with GitHub you need a GitHub account and to install Git.

Sign up for GitHub here

After that you need to create your project repo. That can be done here. All you do is give it a name.

After this you can create your repo locally or use an existing repo.

Example:
This is creating a new 100% empty project.
Code:
touch README.md git init git add README.md git commit -m "first commit" git remote add origin git@github.com:USERNAME/REPONAME.git

This is using a preexisting local repo
Code:
git remote add origin git@github.com:USERNAME/REPONAME.git
git push -u origin master
[/code]

Now you can push to your projects on GitHub!


In Closing

You can now use local repos as well as push to remote GitHub Projects. If you need any help don't be afraid to ask. Git is an amazing tool and VERY powerful, which can be daunting, but after some use it is all very natural.


more info
- https://help.github.com/articles/set-up-git
- http://git-scm.com/docs/gittutorial
- ~$ man git
[Image: iQ3pcQu.png]
BTC Address: 1DCKgDaWcmc9dxBkhe9qrTQtrQpoFUzXdn

[+] 1 user Likes h3r0's post
Reply

RE: Git and GitHub for developing and sharing code #2
Git is a great resource for any developer, thanks for the share.

Reply

RE: Git and GitHub for developing and sharing code #3
Excellent tutorial, very straightforward and reads well.

Reply

RE: Git and GitHub for developing and sharing code #4
Very Good work and nice thread I am impressed by the amount of work you have put and explained a lot of things. I hope you become active on HC. Looking forward to your future contributions.

Well Some programmers on HC including me do have Github.
[Image: OilyCostlyEwe.gif]

Reply

RE: Git and GitHub for developing and sharing code #5
This is really interesting, we needed a tutorial about the subject here in HC, thanks for making it (good job)!

And I agree with @Psycho_Coder (as most of the time), we do use Github, and some of us are really active there!

Thanks again for the great tutorial.

Peace
[Image: wvBFmA5.png]

Reply

RE: Git and GitHub for developing and sharing code #6
I'm glad you all are finding my tutorial helpful and that it isn't written too horribly. Near the end of my short novel that I wrote here(Around line 190), I started getting lazy. I'll most likely edit it, but if anyone needs deeper explaining I'm more than happy to help as well as update the tutorial on the section you had difficulty with.
[Image: iQ3pcQu.png]
BTC Address: 1DCKgDaWcmc9dxBkhe9qrTQtrQpoFUzXdn

Reply

RE: Git and GitHub for developing and sharing code #7
Little something to share here... by Linus Torvalds

[Image: wvBFmA5.png]

Reply







Users browsing this thread: