Create Local Repository
Assume you already have a github account and a local repository created by command git init
under a directory path. And the important thing is that you already have setted SSH connection between github and local git repository.
gehan@gehan-Lenovo-G480:~/shell$ ls -al
total 32
drwxrwxr-x 2 gehan gehan 4096 6月 29 21:52 .
drwxr-xr-x 39 gehan gehan 4096 6月 29 21:52 ..
-rwxrw-r-- 1 gehan gehan 262 6月 29 21:51 check.sh
-rwxrw-r-- 1 gehan gehan 483 6月 29 21:51 display.sh
-rw-rw-r-- 1 gehan gehan 106 6月 29 21:51 input.txt
-rw-rw-r-- 1 gehan gehan 72 6月 29 21:51 sample.sed
-rwxrw-r-- 1 gehan gehan 239 6月 29 21:51 searchMaxfile.sh
-rw-rw-r-- 1 gehan gehan 385 6月 29 21:51 summary.awk
gehan@gehan-Lenovo-G480:~/shell$ git init
Initialized empty Git repository in /home/gehan/shell/.git/
gehan@gehan-Lenovo-G480:~/shell$ ls -al
total 36
drwxrwxr-x 3 gehan gehan 4096 6月 29 21:53 .
drwxr-xr-x 39 gehan gehan 4096 6月 29 21:52 ..
-rwxrw-r-- 1 gehan gehan 262 6月 29 21:51 check.sh
-rwxrw-r-- 1 gehan gehan 483 6月 29 21:51 display.sh
drwxrwxr-x 7 gehan gehan 4096 6月 29 21:53 .git
-rw-rw-r-- 1 gehan gehan 106 6月 29 21:51 input.txt
-rw-rw-r-- 1 gehan gehan 72 6月 29 21:51 sample.sed
-rwxrw-r-- 1 gehan gehan 239 6月 29 21:51 searchMaxfile.sh
-rw-rw-r-- 1 gehan gehan 385 6月 29 21:51 summary.awk
gehan@gehan-Lenovo-G480:~/shell$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
check.sh
display.sh
input.txt
sample.sed
searchMaxfile.sh
summary.awk
nothing added to commit but untracked files present (use "git add" to track)
gehan@gehan-Lenovo-G480:~/shell$ git add -A
gehan@gehan-Lenovo-G480:~/shell$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: check.sh
new file: display.sh
new file: input.txt
new file: sample.sed
new file: searchMaxfile.sh
new file: summary.awk
gehan@gehan-Lenovo-G480:~/shell$ git commit -m 'Init commit'
[master (root-commit) 7c9c13c] Init commit
6 files changed, 86 insertions(+)
create mode 100755 check.sh
create mode 100755 display.sh
create mode 100644 input.txt
create mode 100644 sample.sed
create mode 100755 searchMaxfile.sh
create mode 100644 summary.awk
gehan@gehan-Lenovo-G480:~/shell$ git status
On branch master
nothing to commit, working directory clean
Here we use git init
create a local repository under shell directory and find there are some script files under this directory, then use command git add -A
and git commit
to committed the file into git repository that located at local path.
So the next step we want is to create a remote repository in github, then we can push files from local repository to remote and verse.
Create Remote Repository
First, login the github, and then click + next to your head portrait or the button new repository, screen-shots as below:
After click the new repository button, it will display to guide you create a new remote repository at github.
Fill in a repository name you want in the Repository name text-box, click the button Create repository, then all process will be successful.
Now the situation is: we have locally created a git repository, and we also created a git repository at GitHub. Then hope the two repositories can remote synchronization, such github repository can be as a backup, at same time other people can realize collaboration through the remote repository .
Push Files into Github
At present, the remote repository Linuxshell at github is empty. And hence we can clone a new repository from it or make a associated with an existing local repository, then we can push content into github repository. Its realize command like below:
gehan@gehan-Lenovo-G480:~/shell$ git remote add origin https://github.com/myxyhg/Linuxshell.git
gehan@gehan-Lenovo-G480:~/shell$ git push -u origin master
Username for 'https://github.com': myxy_hg@163.com
Password for 'https://myxy_hg@163.com@github.com':
To https://github.com/myxyhg/Linuxshell.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/myxyhg/Linuxshell.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
As above, we use git remote add origin https://github.com/myxyhg/Linuxshell.git
to associated local and remote repository, but we get an error when push files into remote repository via command git push -u origin master
, it shows error: failed to push some refs to ‘https://github.com/myxyhg/Linuxshell.git’.
How to fix it?
When meet this error, we can type git pull origin master
firstly to pull files from github remote server, then input
git push origin master
, it will be ok.
gehan@gehan-Lenovo-G480:~/shell$ git pull origin master
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/myxyhg/Linuxshell
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
Merge made by the 'recursive' strategy.
README.md | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 README.md
gehan@gehan-Lenovo-G480:~/shell$ ls -al
total 40
drwxrwxr-x 3 gehan gehan 4096 6月 29 21:55 .
drwxr-xr-x 39 gehan gehan 4096 6月 29 21:52 ..
-rwxrw-r-- 1 gehan gehan 262 6月 29 21:51 check.sh
-rwxrw-r-- 1 gehan gehan 483 6月 29 21:51 display.sh
drwxrwxr-x 8 gehan gehan 4096 6月 29 21:56 .git
-rw-rw-r-- 1 gehan gehan 106 6月 29 21:51 input.txt
-rw-rw-r-- 1 gehan gehan 36 6月 29 21:55 README.md
-rw-rw-r-- 1 gehan gehan 72 6月 29 21:51 sample.sed
-rwxrw-r-- 1 gehan gehan 239 6月 29 21:51 searchMaxfile.sh
-rw-rw-r-- 1 gehan gehan 385 6月 29 21:51 summary.awk
gehan@gehan-Lenovo-G480:~/shell$ git status
On branch master
nothing to commit, working directory clean
gehan@gehan-Lenovo-G480:~/shell$ git push origin master
Username for 'https://github.com': myxy_hg@163.com
Password for 'https://myxy_hg@163.com@github.com':
Counting objects: 11, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 1.62 KiB | 0 bytes/s, done.
Total 10 (delta 1), reused 0 (delta 0)
To https://github.com/myxyhg/Linuxshell.git
6e7b73d..445370f master -> master
But if you get other errors like fatal: Couldn’t find remote ref master or fatal: ‘origin’ does not appear to be a git repository or fatal: Could not read from remote repository, you need to type git remote add origin https://github.com/myxyhg/Linuxshell.git
again.