Upload Project code to github using Git

Source: Internet
Author: User
Tags diff file diff git client using git git commands

GitHub is a git-based code hosting platform, where paid users can build private warehouses, and our general free users can only use public repositories, which means the code is open. This is enough for the average person to have a public warehouse.

Register your account and create a warehouse

The first step to using GitHub is, of course, registering a GitHub account. After that you can create a warehouse (free users can only build public warehouses), create a New Repository, fill in the name after create, there will be some warehouse configuration information, this is a simple git tutorial. The steps are as follows:

Open https://github.com Register directly on the home page



Click button to register successfully, select "+" on the user menu above the page->new repository create a new warehouse


Take a name for the warehouse, click the Create Warehouse button you will successfully create a warehouse

Installing the GIT client

GitHub is a server, and we need a git client to use git on our own computer.

Windows users please download http://msysgit.github.com/

Mac users please download http://code.google.com/p/tortoisegit/

All the way next, after the installation succeeds, go back to the C drive, or any folder, click the right mouse button will be a few more menus
such as Git Init hear, git Bash, git Gui, the installation is successful.

Configure Git

Let's find a place in the computer's hard drive to store the local warehouse, for example, we set up the local repository under the C:\MyRepository\1ke_test folder

To enter the 1ke_test folder, right-click the following steps:

1) in the local repository, right-click Git Init here to get a more. Git folder, which means that local git was created successfully. Right-click Git bash into the git command line with the following effect:

For the sake of insurance, we first execute the GIT init command

$ git init

To upload a local repository to GitHub, you need to configure SSH key.

2) Create an SSH key locally

"[email protected]"

The following [email protected] changed to your mailbox. My email address is [email protected], which is also registered on GitHub:

To enter directly, the instructions will generate SSH key on the default file Id_rsa.

Then the system asks for a password, directly press ENTER to indicate that no password is set

Repeat the password is also a direct carriage return, after prompting you shh key has been generated successfully.

Then we go to the prompt address to view the SSH key file. The address of my computer is C:\Users\lilu\.ssh, where Lilu is the name of my Computer

Open the Id_rsa.pub and copy the key inside. The key inside is a pair of characters that can not understand the combination of numbers, do not control it, directly copied.

Go back to the GitHub website, go to account Settings, and choose SSH keys,add ssh Key on the left.

Title fill, paste key.

3) Verify success, enter in Git bash

$ ssh -T [email protected]

Enter will see: You've successfully authenticated, but GitHub does not provide shell access. This means that you have successfully connected to GitHub.

4) The next thing we need to do is to upload the local repository to GitHub, and we'll have to set up username and email before that, because GitHub will record them every time it commits.

"your name"$ git config --global user.email "[email protected]"

Enter the above command line, and my interface is displayed as follows

5) Go to the repository you want to upload, right-click Git Bash, add remote address

$ git remote add origin [email protected]:yourName/yourRepo.git

The following yourname and Yourrepo indicate that you re-github user name and the newly created warehouse, added after the addition of the. git, open config, here will be a remote "origin" content, this is just added to the long-range address, You can also modify config directly to configure the remote address.

corresponding to GitHub

Submit Upload

1) Next add some files to the local repository, such as the Readme

Create a new Readme file locally

Then enter the command at the command line

"first commit"

My execution interface is as follows

2) upload to GitHub

$ git push origin master

The git push command pushes the local repository to the remote server.

The git pull command is the opposite.

Note: First commit, Git pull, after modifying the code, use GIT status to view the differences in the file, using git add to add the file to commit.

You're done, now you know how to submit your local project to GitHub.

Gitignore file

Gitignore as the name implies is to tell git to ignore the files, this is a very important and useful file. In general, we write the code after the compilation, debugging and other operations, which will produce a lot of intermediate files and executables, these are not code files, do not need git to manage. We see a lot of these files in git status, and if we add them with Git add-a, it's too cumbersome to add them manually. Then we need to gitignore.

git commands

View, add, submit, delete, retrieve, reset modified files

GitHelp <Command># Show Command Helpgit show# Show content for a commit git show $idgit co--<file># Discard workspace Modify Git Co.# Discard workspace Modify git add <file> # commit working file modifications to local staging area git Add. # submit all modified work files staging area git rm <file> # delete files from repository git rm <file>- -cached # delete files from repository, but do not delete files git reset <file> # Restore from staging area to working file git reset--. # Restore from staging area to working file git reset--hard # restore the state that was last submitted, That is, discard all changes to git ci <file> git ci after the last commit.                                    Git ci -a # git Add, git rm and git ci are all combined to do Git ci-am "Some comments" git ci--amend # modify last commit record git revert < $id > # restore the state of a commit, the restore action itself also creates a commit object git revert HEAD # restore status of last commit            


View File diff

# 比较当前文件和暂存区文件差异 git diffgit diff <id1><id2> # 比较两次提交之间的差异git diff <branch1>..<branch2> # 在两个分支之间比较git diff --staged # 比较暂存区和版本库差异git diff --cached # 比较暂存区和版本库差异git diff --stat # 仅仅比较统计信息




View Commit Record

log git log <file> # 查看该文件每次提交记录git log -p <file> # 查看每次详细修改内容的diffgit log -p -2 # 查看最近两次详细修改内容的diffgit log --stat #查看提交统计信息


Tig
You can use TIG instead of diff and Log,brew install TIG on your Mac


Git Local Branch Management
View, toggle, create, and delete branches

Git br-r# View remote branch git br <new_branch> # Create new branch git br-v # View the last commit information for each branch git br--merged # View the branch that has been merged into the current branch git br--no-merged # View the branch that has not been merged into the current branch git co <branch> # switch to a branch git Co-b <new_branch> # Create a new branch and switch past git co-b <new_branch> <branch> # Create a new New_branchgit co # to checkout a history commit record, But without branching information, switching to another branch will automatically delete the Git co  $id-B <new_branch> # Make a history commit record checkout out, create a branch git br -d <branch> # Delete a branch git br-d <branch> # Force a branch to be removed (the branch that has not been merged is removed when it needs to be forced)     



Branch Merging and Rebase

# 将branch分支合并到当前分支git merge origin/master --no-ff # 不要Fast-Foward合并,这样可以生成merge提交git rebase master <branch> # 将master rebase到branch,相当于: git co <branch> && git rebase master && git co master && git merge <branch>



git patch management (for easy development of synchronization on multiple machines)

# 生成补丁git apply ../sync.patch # 打补丁git apply --check ../sync.patch #测试补丁能否成功




Git staging management

# 暂存git stash list # 列所有stashgit stash apply # 恢复暂存的内容git stash drop # 删除暂存区Git远程分支管理git pull # 抓取远程仓库所有分支更新并合并到本地git pull --no-ff # 抓取远程仓库所有分支更新并合并到本地,不要快进合并git fetch origin # 抓取远程仓库更新git merge origin/master # 将远程主分支合并到本地当前分支git co --track origin/branch # 跟踪某个远程分支创建相应的本地分支git co -b <local_branch> origin/<remote_branch> # 基于远程分支创建本地分支,功能同上



Git push # push all branches

# 将本地主分支推到远程主分支git push -u origin master # 将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库)git push origin <local_branch> # 创建远程分支, origin是远程仓库名git push origin <local_branch>:<remote_branch> # 创建远程分支git push origin :<remote_branch> #先删除本地分支(git br -d <branch>),然后再push删除远程分支




GIT Remote repository Management

# 查看远程服务器地址和仓库名称git remote show origin # 查看远程服务器仓库状态git remote add origin [email protected] github:robbin/robbin_site.git # 添加远程仓库地址git remote set-url origin [email protected] github.com:robbin/robbin_site.git # 设置远程仓库地址(用于修改远程仓库地址) git remote rm <repository> # 删除远程仓库



Create a remote Warehouse

clone --bare robbin_site robbin_site.git # 用带版本的项目创建纯版本仓库scp -r my_project.git [email protected] git.csdn.net:~ # 将纯仓库上传到服务器上mkdir robbin_site.git && cd robbin_site.git && git --bare init # 在服务器创建纯仓库git remote add origin [email protected] github.com:robbin/robbin_site.git # 设置远程仓库地址git push -u origin master # 客户端首次提交git push -u origin develop # 首次将本地develop分支提交到远程develop分支,并且trackgit remote set-head origin master # 设置远程仓库的HEAD指向master分支




You can also command settings to track remote libraries and local libraries

git branch --set-upstream master origin/master

git branch --set-upstream develop origin/develop 此文章属转载,原文地址:http://1ke.co/course/194 另附: 解决 在使用git 对源代码进行push到gitHub时可能会出错,error: failed to push some refs to git。出现错误的主要原因是github中的README.md文件不在本地代码目录中可以通过如下命令进行github与本地代码合并: git pull --rebase origin master重新执行之前的git push 命令,成功!

Reprint: http://blog.csdn.net/llf369477769/article/details/51917557

Upload Project code to github using Git

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.