Skip to content
Jason Charney edited this page Apr 18, 2016 · 4 revisions

Git is a source code management or SCM system that was originally developed by Linux developer Linus Torvalds. SCM systems are typlically used for version control for software development.

Git is what powers this website. There are alternatives like Mercurial, Subversion, and Concurrent Subversion System (CVS), however Git is currently the king at the moment.

Sorry, no add-apt-repository

One of the many things that grinds my gears about any Linux repository is that sometimes the software that should be installed that is stable and won't break anything else if in install it is never installed because the folks who wrote the script to fetch packages never get them from new sources.

Sadly, Raspbian happens to be one of those distros as it's version of apt-get doesn't come with add-apt-repository. If it had, these are the instructions I would have gave for downloading the most recent stable version of Git. (A must have that I'll just have to figure out some other time.) You still have git, by the way, just not a better nicer version of it.

$ sudo add-apt-repository ppa:git-core/ppa
$ sudo apt-get update
$ sudo apt-get install git
$ git --version                                  # git version 2.6.4

Setting Up Git

Installing Git has pretty much been done for you on most Debian or Fedora based Linux distro. All that really needs to be done is the set up for a .gitconfig file in your home directory.

[user]
	name = Your Name
	email = your@email.com
[core]
	editor = vim
	eol = lf
[color]
	ui = true
[push]
	default = simple

I'll eventually post some curl instructions to download that dummy file for you to fill out the name and email fields later.

Alternatively, you can use

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
$ git config --global push.default simple
$ git config --global core.editor vim
$ git config --global core.eol lf
$ git config --global color.ui true
$ git config --list                                      # see settings that were set

But doing the the first way is OK and completes a few important items like setting the editor, the end of line, and showing colors.

Use git to clone a project

The easiest thing you can do with git is clone a project. Pretty much, all you do is download the project as well as any changes that occurred to it.

I suggest putting all the git projects you make or contribute in a folder called ~/Projects and all the projects you just downloaded to use to ~/Software. In the ~/Software folder, I also suggest putting any projects created by someone else into a folder with the creators name, as you will likely encounter similarly named Git projects

cd ~/Software
mkdir someuser
cd someuser
git clone https://github.com/someuser/someproject.git    # The project is stored in someproject

Creating a git project

Let $USERNAME be your Github username, $PASSWORD be your Github password, and $REPONAME be the name of the repository directory, or repo, of your project that you wish to establish.

Remember my recommentations about your projects. Our first step is to got to the ~/Projects folder.

$ cd ~/Projects										# Step 1
$ mkdir $REPONAME && cd $REPONAME							# Step 2
$ vim write_some_file_and_save_it.sh							# Step 3
$ curl -u "$USERNAME" https://api.github.com/user/repos -d '{"name":"$REPONAME"}'	# Step 4
$ git init										# Step 5
$ git status										# Step 6
$ git add .										# Step 7
$ git status										# Step 8
$ git commit -m "First commit"								# Step 9
$ git remote add origin https://github.com/$USERNAME/$REPONAME				# Step 10
git remote -v										# Step 11
$ git push origin master								# Step 12

What happened here?

  1. We first wen to the Projects directory. This is your project, anyway.
  2. Make and go to the $REPONAME directory.
  3. Create a file, or add files to this directory. Generally, most people will create a README.md file first so that when visitors visit your repository, they know what it is about. Try to postpone creating a .gitignore file until after the first commit. Avoid posting anything that has private information like passwords or personal information. Remember, GitHub repos are public by default.
  4. Create the repository on GitHub. You will need to use [curl](curl and tar). Type this command then enter your password. You could have entered the -u option as -u "$USERNAME:$PASSWORD" but that's really not secure as your password can appear in your Bash history. So do it -u "$USERNAME"` and so on, then enter your password. I'll talk about this step later.
  5. Initialize a new git repo on your system. A .git hidden directory will be created with basic files need to use this directory with the repo.
  6. This step really isn't necessary but it does show what files could be added to your repository before the next commit. This is known as the staging area. Before you commit a file, it is staged.
  7. Add files to the git repo commit. This will set up your file for the next commit. You can do this to the same file plenty of times before the next commit so as long as it is done before the commit. You can edit those files after the commit, but I'll explain why when we get to the commit step. To commit all the current files use git add .. For a specific file, use git add $filename where $filename is a filename, obviously.
  8. See what happened in the staging area. We can also remove files from the staging area using git rm which will signal the removal of a file at the next commit.
  9. Commit your files. This is not the same as uploading. This sets a milestone know as a commit. When you commit, you must include a message using the -m option. If you don't, your text editor will open and demand you explain what is going on.
  10. Add a remote. The remote is called 'origin'. I'm still trying to understand this part, but what I do know is that this will set up your repo to set up the URL you need to post (push) or get (pull/fetch) files. Do not end your Repo with .git. That's a bit archaic, and although still supported, old habits die hard. If you are creating a repo for the wiki of a repo, you should create a sibling directory called $REPONAME.wiki in the parent directory, and repeat this tutorial for that, but create a 'Home.md' file. I'm still trying to thing of what to do for a Github pages directory ($USERNAME.github.io). Stay tuned for that. I want to use Jekyll for that.
  11. Verify that your repo is set up. This will show what url will be used when you git fetch (or git pull) or git push.
  12. Push your commit. The upload! If successful, the files in your first commit should be online.

In the curl step you may have see the JSON output in this format. It would look something like what is after this next bulleted list and paragrah where the following variables are defined.

  • $REPOID is the id number of the repo.
  • $REPONAME is the name of the repo.
  • $USERID is the user id number of your Github account.
  • $USERNAME is the user name of your Github account.
  • $TIMESTAMP is a timestamp that is in $(date -u +'%Y-%m-%dT%H:%M:%SZ') or $(date -u +'%FT%TZ') format.

The more you define your variables, the better as you can see there are somethings that were not defined when we used the curl command. This can be remedied writing a script but I don't feel like doing that right now. At any rate, here's what the JSON output of the curl command would look like.

{
  "id": $REPOID,
  "name": "$REPONAME",
  "full_name": "$USERNAME/$REPONAME",
  "owner": {
    "login": "$USERNAME",
    "id": $USERID,
    "avatar_url": "https://avatars.githubusercontent.com/u/$USERID?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/$USERNAME",
    "html_url": "https://github.com/$USERNAME",
    "followers_url": "https://api.github.com/users/$USERNAME/followers",
    "following_url": "https://api.github.com/users/$USERNAME/following{/other_user}",
    "gists_url": "https://api.github.com/users/$USERNAME/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/$USERNAME/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/$USERNAME/subscriptions",
    "organizations_url": "https://api.github.com/users/$USERNAME/orgs",
    "repos_url": "https://api.github.com/users/$USERNAME/repos",
    "events_url": "https://api.github.com/users/$USERNAME/events{/privacy}",
    "received_events_url": "https://api.github.com/users/$USERNAME/received_events",
    "type": "User",
    "site_admin": false
  },
  "private": false,
  "html_url": "https://github.com/$USERNAME/$REPONAME",
  "description": null,
  "fork": false,
  "url": "https://api.github.com/repos/$USERNAME/$REPONAME",
  "forks_url": "https://api.github.com/repos/$USERNAME/$REPONAME/forks",
  "keys_url": "https://api.github.com/repos/$USERNAME/$REPONAME/keys{/key_id}",
  "collaborators_url": "https://api.github.com/repos/$USERNAME/$REPONAME/collaborators{/collaborator}",
  "teams_url": "https://api.github.com/repos/$USERNAME/$REPONAME/teams",
  "hooks_url": "https://api.github.com/repos/$USERNAME/$REPONAME/hooks",
  "issue_events_url": "https://api.github.com/repos/$USERNAME/$REPONAME/issues/events{/number}",
  "events_url": "https://api.github.com/repos/$USERNAME/$REPONAME/events",
  "assignees_url": "https://api.github.com/repos/$USERNAME/$REPONAME/assignees{/user}",
  "branches_url": "https://api.github.com/repos/$USERNAME/$REPONAME/branches{/branch}",
  "tags_url": "https://api.github.com/repos/$USERNAME/$REPONAME/tags",
  "blobs_url": "https://api.github.com/repos/$USERNAME/$REPONAME/git/blobs{/sha}",
  "git_tags_url": "https://api.github.com/repos/$USERNAME/$REPONAME/git/tags{/sha}",
  "git_refs_url": "https://api.github.com/repos/$USERNAME/$REPONAME/git/refs{/sha}",
  "trees_url": "https://api.github.com/repos/$USERNAME/$REPONAME/git/trees{/sha}",
  "statuses_url": "https://api.github.com/repos/$USERNAME/$REPONAME/statuses/{sha}",
  "languages_url": "https://api.github.com/repos/$USERNAME/$REPONAME/languages",
  "stargazers_url": "https://api.github.com/repos/$USERNAME/$REPONAME/stargazers",
  "contributors_url": "https://api.github.com/repos/$USERNAME/$REPONAME/contributors",
  "subscribers_url": "https://api.github.com/repos/$USERNAME/$REPONAME/subscribers",
  "subscription_url": "https://api.github.com/repos/$USERNAME/$REPONAME/subscription",
  "commits_url": "https://api.github.com/repos/$USERNAME/$REPONAME/commits{/sha}",
  "git_commits_url": "https://api.github.com/repos/$USERNAME/$REPONAME/git/commits{/sha}",
  "comments_url": "https://api.github.com/repos/$USERNAME/$REPONAME/comments{/number}",
  "issue_comment_url": "https://api.github.com/repos/$USERNAME/$REPONAME/issues/comments{/number}",
  "contents_url": "https://api.github.com/repos/$USERNAME/$REPONAME/contents/{+path}",
  "compare_url": "https://api.github.com/repos/$USERNAME/$REPONAME/compare/{base}...{head}",
  "merges_url": "https://api.github.com/repos/$USERNAME/$REPONAME/merges",
  "archive_url": "https://api.github.com/repos/$USERNAME/$REPONAME/{archive_format}{/ref}",
  "downloads_url": "https://api.github.com/repos/$USERNAME/$REPONAME/downloads",
  "downloads_url": "https://api.github.com/repos/$USERNAME/$REPONAME/downloads",
  "issues_url": "https://api.github.com/repos/$USERNAME/$REPONAME/issues{/number}",
  "pulls_url": "https://api.github.com/repos/$USERNAME/$REPONAME/pulls{/number}",
  "milestones_url": "https://api.github.com/repos/$USERNAME/$REPONAME/milestones{/number}",
  "notifications_url": "https://api.github.com/repos/$USERNAME/$REPONAME/notifications{?since,all,participating}",
  "labels_url": "https://api.github.com/repos/$USERNAME/$REPONAME/labels{/name}",
  "releases_url": "https://api.github.com/repos/$USERNAME/$REPONAME/releases{/id}",
  "deployments_url": "https://api.github.com/repos/$USERNAME/$REPONAME/deployments",
  "created_at": "$YYYY-$MM-$DDT<hh>:<mm>:<ss>Z",
  "updated_at": "$YYYY-$MM-$DDT<hh>:<mm>:<ss>Z"",
  "pushed_at": "$YYYY-$MM-$DDT<hh>:<mm>:<ss>Z"",
  "git_url": "git://github.com/$USERNAME/$REPONAME.git",
  "ssh_url": "git@github.com:$USERNAME/$REPONAME.git",
  "clone_url": "https://github.com/$USERNAME/$REPONAME.git",
  "svn_url": "https://github.com/$USERNAME/$REPONAME",
  "homepage": null,
  "size": 0,
  "stargazers_count": 0,
  "watchers_count": 0,
  "language": null,
  "has_issues": true,
  "has_downloads": true,
  "has_wiki": true,
  "has_pages": false,
  "forks_count": 0,
  "mirror_url": null,
  "open_issues_count": 0,
  "forks": 0,
  "open_issues": 0,
  "watchers": 0,
  "default_branch": "master",
  "permissions": {
    "admin": true,
    "push": true,
    "pull": true
  },
  "network_count": 0,
  "subscribers_count": 1
}

Of course you could always go to your repo on Github and clone your repo to your Projects directory and pull any changes (if you need to), but doing it the way I described it give you some serious street cred.

Making changes to a git project

"In case of fire: git commit. git push. git out!" --Some T-shirt

That last section was rather wordy. Fortunately, you don't need to do much to make changes.

$ git add $filename				# or some other git commands.  You can use more than one before the next commit.
$ git commit -m "Your spectacular message"	# commit your changes
$ git push					# In this form, the committed changes will be made from the origin to the master branch.

Merging with other projects

TODO: Can I also change the name of a project?

See also

External links

Setup

  1. [Assemble the Hardware](Assemble the Hardware)
  2. [Install the Software](Install the Software)
  3. 🆙 [Setup your Raspberry Pi](Setup your Raspberry Pi)
  4. [Download the Missing Parts](Download the Missing Parts)

Typical Utilities

  • [Downloading and extracting with curl and tar](curl and tar)
  • [Browsing with ls and cat](ls and cat)
  • [Searching with grep and find](grep and find)
  • [Filtering with sed and awk](sed and awk)
  • [Piping with less, pv, and tee](less, pv, and tee)
  • Monitor your system with htop
  • Multiplex with tmux

Clone this wiki locally