Basic Guide to Git on Github (Ubuntu)

Jan 26, 2019

Install Git

sudo apt-get install git

Check git version

git version

NOTE: Refer to git download on other platform.

Setup Git with Github

Create github account.

Setup username and email.

git config --global user.name "github_username"git config --global user.email "github_email"

Check configuration

git config --list

Edit configuration

git config --edit --global

Add SSH key to github

Check if you already have a local SSH key. If yes, ~/.ssh/id_rsa should exist.

ls -al ~/.ssh

If you don't have a local SSH key, generate one.

ssh-keygen -t rsa -b 4096 -C "any identifier: email, etc."

NOTE: I would recommend to use a passphrase

Add SSH key to ssh-agent

ssh-add ~/.ssh/id_rsa

List all ssh-agent keys.

ssh-add -l

Copy content of ~/.ssh/id_rsa.pub

sudo apt-get install xclipxclip -sel clip < ~/.ssh/id_rsa.pub

Paste SSH key to Github. Visit Github, click to top right user profile icon, then Settings -> SSH and GPG keys -> New SSH Keys.

NOTE: Refer to Connecting to GitHub with SSH.

Create New Github Repository

Visit Github, click on the top right + icon (left of user profile icon) and select New Repository. Fill in the following:

  • Name: e.g. git-test
  • Description: optional
  • Public/Private
  • Initialize this repository with a README (checked this if you haven't created the project files on your computer yet)
  • Add .gitignore: e.g. select python if you are doing a python project
  • Add license: if this is a public project, 3 common choices are Apache License 2.0, GNU General Public License v3.0, MIT License. For private project, you can select None.

NOTE: Refer to Which License for Sample Code for license reference.

Checkout a Github Repository

Download/Clone the Github repository to a local directory.

Run the following in your workspace directory.

git clone [email protected]:USERNAME/git-test.git

NOTE: The above will create git-test directory in the current directory.

NOTE: You can rename git-test directory with git clone [email protected]:USERNAME/git-test.git NEW_DIR_NAME.

NOTE: If you prefer to create a local git repository without cloning existing remote repository, use git init.

Add, Commit & Push

  • Add: start tracking a file
  • Commit: commit to local repository (revision log created)
  • Push: push changes to remote repository (e.g. Github)

Check current git status

git status

Add new file

git add test.py

Add all files (new, modified, deleted).

git add -A

NOTE: git add --all or git add . has the same effect.

Commit all tracked files.

git commit -m "Commit #1"

NOTE: Comment/Message for commit are mandatory. You can use git commit -m. (. as message) or git commit -am'save' (save as message).

NOTE: Some might use git commit -am 'message' to combine add and commit, but this only affect files that have been modified and deleted, but doesn't affect new files.

Push all committed changes to server.

git push origin master

Branch

Sometimes you want to work on a big feature which might take more than one day, yet you still want access to the original source (before modification) still be accessible and stable.

Create a new branch and switch to the branch.

git checkout -b sprint-01

Show available branch and current branch.

git branch
  master
* sprint-01

Push branch to remote server (e.g. Github)

git push origin sprint-01

Switch branch

git checkout master

Delete branch

git branch -d sprint-01

Merge

Once your work on the branch is completed and tested, you can merge it with the master.

Assuming you are working in the branch (e.g. sprint-01). You can check your current branch

git branch

Check diff with master (assuming you are still in sprint-01 branch).

git diff master

Merge current branch with master

git checkout master     # Switch to `master` branchgit pull                # Optional: get latest update from remote repositorygit merge sprint-01     # Merge `master` with `sprint-01`git push origin master  # Update merged `master` to remote repository

Tag

Tag is usually used for release. It is like a save point, where we can refer back to this point for deployment or testing.

Create tag.

git tag -a 0.1 -m "Message for 0.1"

Update tag to remote repository.

git push origin 0.1

Show created tags.

git tag

Delete tag

git tag -d 0.1 

Conclustion

This is a very basic guide suited for single user.

Git is quite simple to get started, but hard to master especially when there are more users in a single project.

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.