Lab 1 Git and Github operations

Revisit Git operations

init a git repository

Git is a folder based version control system. To create an empty Git repository or reinitialize an existing repository, we could use git init command.

mkdir new_project_folder && cd new_project_folder
git init

We first make a new folder by mkdir and cd to it. We could use git init to initialize an empty Git repository. After that, you would see a .git folder created under current directory. You could see it by ls -a, -a option means that showing all files including hidden files.

ls -a

Managing tracked remote repositories

Each Git repository could connected to and tracking other related Git repository. This could be managed by git remote command. Here are three basic usage for git remote

Listing remote repositories

git remote -v

Sample output:

[kenny@portable gahk-public]$ git remote -v
origin  git@github.com:hkbu-kennycheng/gahk-web.git (fetch)
origin  git@github.com:hkbu-kennycheng/gahk-web.git (push

git remote add

git remote add name URL

Example:

git remote add origin https://github.com/hkbu-kennycheng/gahk-web

git remote remove

git remote remove name

Example:

git remote remove origin

git remote rename

git remote rename name new_name
git remote remove origin

Managing files in stage for commit

git add

git add pattern

Here are some common patterns

  • . means all files
  • *.js means all js files
  • folder means all files under the folder
  • folder/*.js means all js files under the folder

git status

You could view the staged files with git status.

git status

git reset

git reset would simply clear all files from stage.

git reset

commit and push

commit

git commit -m "message"

push

git push -u remote branch

Example:

git push -u origin master

More Github features

Access Github with SSH keys

If you don’t like to login each time when doing git clone, git push and git pull, you need to generate a key and upload it on Github.

enter image description here

Please click on New SSH key, you could give it a name and input the key.

enter image description here

To generate a SSH key in your local machine, please issue the following command in terminal.

ssh-keygen

Using cat command as follows could make the public key appear in terminal and you could copy it to Github.

cat ~/.ssh/id_rsa.pub

Fork repository

Fork means cloning a repository from other and continue to work on it by yourself. It may or may not merge back to original depending on your need.

enter image description here

To fork a repository on Github, simply click on the top right Fork button. You will given a list for selecting destination of your fork.

enter image description here

After that, you would have your own copy of the repository.

Collaboration

After repository created, project leader/manager would enable access for project team members to access the repository.

To do so on Github repository, please click on Settings tab on the top bar.

Github Topbar

Then, please click Collaborators on left menu. Input Github username of your team member and click Add Collaborator.

enter image description here

After adding team members to the repository, let’s click on Project tab in your repository top bar.

Sending pull request

To create a pull request, please click on New pull request button.

enter image description here

Then you would have a screen to select forks and branches. You could click on compare across forks to select branches in other forks.

enter image description here

The merging direction is from right to left, meaning that you are merging code on the right side branch to left side branch. If you are the owner of left side repository, you could directly merge the commits to your branch. If you are not the owner, a pull request would created in the left side repository waiting for repository owner to approval. The repository owner would receive a notification for reviewing your pull request.

enter image description here

Merging pull request

If someone sent you a pull request, you could see a counter in the Pull requests tab on your Github repository page.

enter image description here

Click on the tab and you would navigated to pull requests page.

enter image description here

Click in a pull request, you would see the details of the changes.

enter image description here

Simply click on Merge pull request to merge the changes into your repository.

enter image description here

There are several options for your merge.

  • Create a merge commit means simply merging all changes. If the request contains several commits, it would create the same number of commit in your repository.
  • ** Squash and merge** means merging all change as a single commit.
  • For ** Rebase and merge**, all commits from the topic branch (or head branch) are added onto the base branch individually without a merge commit.

Rebase

          A---B---C topic
         /
    D---E---F---G master

would becomes:

                  A'--B'--C' topic
                 /
    D---E---F---G master

More Git operations

Clone and checkout remote branch

Checkout directly while clone

We could checkout a particular branch while clone by adding -b branch_name option.

git clone https://your_repo_url -b branch_name

It just behave the same as following commands basically.

git clone https://your_repo_url
git checkout -b branch_name -t origin/branch_name

The -b branch_name option in git checkout means create a branch with branch_name locally. For -t origin/branch_name means set the newly created branch to track remote branch origin/branch_name.

Checkout manually after cloning

git clone https://your_repo_url
cd your_project_location
git checkout -b kenny -t origin/kenny

Basic Branching

A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master . As you start makingcommits, you’re given a master branch that points to the last commit you made. Every time you commit, it moves forward automatically.

After cloning the repository to local computer, you could create a new branch for development. To create a new branch, we use git checkout command. Here is an example usage of this command. It would create a new branch with name iss53 in your local repository.

enter image description here

git checkout -b iss53

After you did some changes and committed, you could push your local branch to remote by the following command.

git push -u origin iss53

git stash: Save uncommitted changes

git stash is a powerful command that help us to save uncommitted changes. It always save us when git fetch fails. Here is a list of git stash sub-commands.

List stash

git stash list

It would provide output like this.

stash@{0}: On branch_name: xxxxx

View stash

git stash show

Example output would like this, showing which file changed with the number of changed lines.

 drivers/media/platform/msm/camera_v2/sensor/Makefile                             |   2 ++
 drivers/misc/tspdrv/ImmVibeSPI.c                                                 |  21 +++++++++++
 drivers/nfc/Kconfig                                                              |   2 ++
 drivers/nfc/Makefile                                                             |   1 +
 drivers/power/qpnp-charger.c                                                     |  84 +++++++++++++++++++++++++++++++++++++++++++
 firmware/synaptics/g3/PLG352-V1.05-PR1648545-DS5.5.1.0-30055185.img              | Bin 0 -> 95744 bytes
 firmware/synaptics/g3/PLG352-V1.08-PR1670515-DS5.5.1.0-30055188.img              | Bin 0 -> 95744 bytes
 firmware/synaptics/g3/PLG391-V1.05-PR1653201-DS5.5.1.0.1066_10055185_S3528A1.img | Bin 0 -> 95744 bytes
 firmware/synaptics/g3/g3_kddi_jp_limit.txt                                       | 272 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 382 insertions(+)

Output stashed changes as patch

We could use git stash show with option -p to format stash as unified patch.

git stash show -p > filename.patch

Basic Merging

After you have completed a single feature of your system, the repository maintainer would merge your commit to the master branch for deployment.

enter image description here

To merge commits from branch iss53, please issue the following commands.

git checkout master
git merge iss53

After that, you would push the master branch to Github for deployment.

git push -u origin master

Download changes from remote

git fetch

git fetch download objects/commits/refs/tags from remote for all branches. It would update remote-tracked branches to latest commit without touching your modification.

git fetch

git pull

git pull is basically same as git fetch && git merge origin/current_branch_name.

Assume the following history exists and the current branch is “master”:

              A---B---C master on origin
             /
        D---E---F---G master
            ^

Then “git pull” will fetch and replay the changes from the remote master branch since it diverged from the local master (i.e., E) until its current commit (C) on top of master and record the result in a new commit along with the names of the two parent commits and a log message from the user describing the changes.

              A---B---C origin/master
             /         \
        D---E---F---G---H master

git pull would fail, if there are any conflicts in merging.