0%

git and github

Git is a software and be installed on different OS, such as Linux, Mac, Windows. Github is a web server which provide a tool for people to use on a website.

on Github

Create a repository
Create a new file
Commit (same as save)
Issue (like comments)
Create an organization
Create branch
Pull request
Merge

Most of the time, you need to work on your local machine to use git. You can clone your repo from github.

On local computer
1
2
3
4
5
6
7
8
9
git clone git@github.com:vmnet8/Hello-World.git
Cloning into 'Hello-World'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 14 (delta 0), reused 0 (delta 0), pack-reused 10
Receiving objects: 100% (14/14), done.
Resolving deltas: 100% (1/1), done.
Checking connectivity... done.

make this a repo located on your local OS
compare with github repo you clone

1
2
3
4
git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
After you make some changes on your local computer, you need to save it into repo, it is not just save, it needs two steps to “save” into repo.for example, add a new line in “README”
1
2
3
4
5
6
7
8
9
10
git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: README.md

no changes added to commit (use "git add" and/or "git commit -a")
1
git add README.md

put this changed file to a stage to wait to be saved in the future.
any changes if want to be saved should be added to this stage first!

1
2
3
git commit README.md -m "add new line under bottom"
[master 37c5b60] add new line under bottom
1 file changed, 1 insertion(+)

only done this, the changed can be “saved” into repo

1
2
3
git remote -v
origin git@github.com:vmnet8/Hello-World.git (fetch)
origin git@github.com:vmnet8/Hello-World.git (push)

Then you can make the changes you made on your local computer to sync to github repo

1
2
3
4
5
6
7
8
9
git push origin master
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 302 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:vmnet8/Hello-World.git
efdcef9..37c5b60 master -> master

If you made changes on github first, how to get your local to sync to github repo?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
git pull origin master 
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:vmnet8/Hello-World
* branch master -> FETCH_HEAD
37c5b60..c99e3a4 master -> origin/master
Updating 37c5b60..c99e3a4
Fast-forward
new file from github | 1 +
1 file changed, 1 insertion(+)
create mode 100644 new file from github

Using other person’s repo

1
2
3
4
5
6
7
git clone git@github.com:RainbowCoder/Rainbow-Song.git
Cloning into 'Rainbow-Song'...
remote: Enumerating objects: 9, done.
remote: Total 9 (delta 0), reused 0 (delta 0), pack-reused 9
Receiving objects: 100% (9/9), done.
Resolving deltas: 100% (1/1), done.
Checking connectivity... done.

you can just make changes on your local, but can’t push it to others repo.
You can sync from other’s repo, but just one direction, from others to your local, not from your change to others.

1
2
3
4
git pull origin master 
From github.com:RainbowCoder/Rainbow-Song
* branch master -> FETCH_HEAD
Already up-to-date.

If you first fork others’s repo to your github, then clone your github url to your local, you can make changes and pull or push to your github. On your github you can pull request to the original repo.

about branch

show the present branch you are

1
2
3
git branch 
branch_newfile
* master

change to other branch

1
2
3
git checkout branch_newfile
Switched to branch 'branch_newfile'
Your branch is up-to-date with 'origin/branch_newfile'.

If you want to see the commit history on local

1
2
3
4
5
6
7
8
9
10
11
12
13
14
git log
commit a43ab8dd9725911488d4e33f4518e9c03c8ee538
Merge: 12021a5 99e5eb0
Author: Anna X <vmnet8@gmail.com>
Date: Sat Sep 7 17:06:46 2019 -0700

all

commit 12021a54f4ab8be1dad823cf428e9b57cac98e0b
Merge: 9dce990 eb4ac1f
Author: Anna X <vmnet8@gmail.com>
Date: Sat Sep 7 16:41:52 2019 -0700

commit all changes
Make a repo on your local computer
1
2
3
4
5
6
7
8
mkdir new_repo
git init
Initialized empty Git repository in /home/anna/github_repo/new_repo/.git/
ll
total 12
drwxrwxr-x 3 anna anna 4096 Sep 9 12:34 .
drwxrwxr-x 16 anna anna 4096 Sep 9 12:34 ..
drwxrwxr-x 7 anna anna 4096 Sep 9 12:34 .git

create a .git file under this dir, it means it created a repo
If you want to sync to github, just login your github account, create an empty repo. Then bond the local and github repo together.

1
2
3
4
git remote  add origin git@github.com:vmnet8/new_repo.git
git remote -v
origin git@github.com:vmnet8/new_repo.git (fetch)
origin git@github.com:vmnet8/new_repo.git (push)