使用Git如何上传代码至github

git命令简介

root@test:/tmp/huhaha# git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
  clone    Clone a repository into a new directory
  init    Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
  add    Add file contents to the index
  mv    Move or rename a file, a directory, or a symlink
  restore    Restore working tree files
  rm    Remove files from the working tree and from the index
  sparse-checkout    Initialize and modify the sparse-checkout
examine the history and state (see also: git help revisions)
  bisect    Use binary search to find the commit that introduced a bug
  diff    Show changes between commits, commit and working tree, etc
  grep    Print lines matching a pattern
  log    Show commit logs
  show    Show various types of objects
  status    Show the working tree status
grow, mark and tweak your common history
  branch    List, create, or delete branches
  commit    Record changes to the repository
  merge    Join two or more development histories together
  rebase    Reapply commits on top of another base tip
  reset    Reset current HEAD to the specified state
  switch    Switch branches
  tag    Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
  fetch    Download objects and refs from another repository
  pull    Fetch from and integrate with another repository or a local branch
  push    Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.

git上传代码操作

  • 拉代码到本地仓库
#克隆一个代码仓到当前环境,链接为你github上链接
#会提示你输入用户名加密码,等待结束即可
root@test:/tmp# git clone http:path.git
Cloning into 'huhaha'...
Username for 'http://xx.xx.xx.xx': user name
Password for 'http://username@xx.xx.xx.xx': password
remote: Enumerating objects: 4066, done.
remote: Counting objects: 100% (4066/4066), done.
remote: Compressing objects: 100% (1673/1673), done.
remote: Total 4066 (delta 2372), reused 4040 (delta 2346)
Receiving objects: 100% (4066/4066), 4.72 MiB | 2.84 MiB/s, done.
Resolving deltas: 100% (2372/2372), done.

  • 创建或切换分支
#这里需要注意,要先切换一下路径
root@test:/tmp# cd huhaha/
#打印一下当前的枝干是主干还是分支
root@test:/tmp/huhaha# git branch
* master
#这里是切换分支操作
root@test:/tmp/huhaha# git checkout huhahah
Branch 'huhahah' set up to track remote branch 'huhahah' from 'origin'.
Switched to a new branch 'huhahah'
#可以看到分支切换过来了
root@test:/tmp/huhaha# git branch
  master
* huahahah

  • 拷贝代码到当前分支对应目录下,并将代码添加到代码仓
#把我新添加的代码目录拷贝到刚刚克隆的代码仓里
root@test:/tmp/huhaha# cp -r /root/huhaha/scripts/test/ /tmp/huhaha/scripts/
#其他需要拷贝的
root@test:/tmp/huhaha# cd scripts/other
#添加代码仓操作
root@test:/tmp/huhaha# git add .   #注意后面的“.”不能丢
#查看一下状态,添加成功则会变颜色
root@test:/tmp/huhaha# git status
On branch huhahah
Your branch is up to date with 'origin/huhahah'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    .......done

  • commit and push代码
#在commit之前还需添加一下邮箱及用户名
root@test:/tmp/huhaha# git config --global user.email "you@example.com"
root@test:/tmp/huhaha# git config --global user.name "Your Name"
#进行commit操作,-m 描述信息
root@test:/tmp/huhaha# git commit -m "add compare case"
[huhahah ed38a60] add compare case
 34 files changed, 2006 insertions(+), 12 deletions(-)
  ......done

#push到github代码仓
root@test:/tmp/huhaha# git push -u origin huhahah
Username for 'http://xx.xx.xx.xx': user name
Password for 'http://username@xxx.xx.xx.xx': password
Enumerating objects: 51, done.
Counting objects: 100% (51/51), done.
Delta compression using up to 24 threads
Compressing objects: 100% (44/44), done.
Writing objects: 100% (44/44), 12.30 KiB | 4.10 MiB/s, done.
Total 44 (delta 35), reused 0 (delta 0)
remote:
remote: To create a merge request for nvme_read_write, visit:
remote:   http://xx.xx.xx.xx/tester/huhaha/merge_requests/new?merge_request%5Bsource_branch%5D=huhahah
remote:
To http://xx.xx.xx.xx/tester/huhaha.git
   81424e3..ed38a60  huhahah -> huhahah
Branch 'huhahah' set up to track remote branch 'huhahah' from 'origin'.


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容