Interactive between local repository and remote repository in git

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  629 21:52 .
drwxr-xr-x 39 gehan gehan 4096  629 21:52 ..
-rwxrw-r--  1 gehan gehan  262  629 21:51 check.sh
-rwxrw-r--  1 gehan gehan  483  629 21:51 display.sh
-rw-rw-r--  1 gehan gehan  106  629 21:51 input.txt
-rw-rw-r--  1 gehan gehan   72  629 21:51 sample.sed
-rwxrw-r--  1 gehan gehan  239  629 21:51 searchMaxfile.sh
-rw-rw-r--  1 gehan gehan  385  629 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  629 21:53 .
drwxr-xr-x 39 gehan gehan 4096  629 21:52 ..
-rwxrw-r--  1 gehan gehan  262  629 21:51 check.sh
-rwxrw-r--  1 gehan gehan  483  629 21:51 display.sh
drwxrwxr-x  7 gehan gehan 4096  629 21:53 .git
-rw-rw-r--  1 gehan gehan  106  629 21:51 input.txt
-rw-rw-r--  1 gehan gehan   72  629 21:51 sample.sed
-rwxrw-r--  1 gehan gehan  239  629 21:51 searchMaxfile.sh
-rw-rw-r--  1 gehan gehan  385  629 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:
create new remote repository
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  629 21:55 .
drwxr-xr-x 39 gehan gehan 4096  629 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  629 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值