The use of Git--how to upload local projects to Github (two simple and convenient methods)

Reprinted from: https://blog.csdn.net/Lucky_LXG/article/details/77849212



Upload your local project to Github (two easy and convenient ways)


1. The first method:

First of all, you need a github account, if you don't have one, go to register first!

https://github.com/

To use git, we need to install the git tool first. The download address is given here. After downloading, you can directly install it all the way:

https://git-for-windows.github.io/

1. Go to the Github homepage and click New repository to create a new project

 2. Fill in the corresponding information and click create 

Repository name: repository name

Description (optional): Description of the warehouse

Public, Private : repository permissions (public sharing, private or designated collaborators)

Initialize this repository with a README: Add a README.md

gitignore: repository type that does not require version management, corresponding to the generated file .gitignore

license: certificate type, corresponding to the generated file LICENSE

 

4. Click Clone or dowload and an address will appear, copy this address for backup.

5. Next, go to the local operation. First, right-click on your project. If you installed git successfully before, two new options will appear on the right-click, namely Git Gui Here and Git Bash Here. Here we select Git Bash Here and enter In the following interface, Test_Bluetooth is my project name.

6. Next, enter the following code (key step) to clone the repository on github to the local

git clone https://github.com/CKTim/BlueTooth.git (replace https://github.com/CKTim/BlueTooth.git with the address you copied earlier)

 7. After this step, there will be an extra folder under your local project folder. The name of the folder is the name of the project on your github. As shown in the figure, I have added a Test folder. We put the local project folder All files (except the newly added folder are not used), and the rest are copied to the newly added folder,

8. Then continue to enter the command cd Test to enter the Test folder

9. Then enter the following codes in turn to complete other remaining operations:

git add . (Note: don't forget the following ., this operation is to add all the files under the Test folder)

git commit -m "commit information" (Note: replace "commit information" with what you need, such as "first commit")

git push -u origin master (Note: the purpose of this operation is to push the local warehouse to github, this step requires you to enter the account and password)

 


Second, the second method:


Step 1: We need to create a local repository (actually a folder).

       You can directly right-click to create a new folder, or you can right-click to open the Git bash command line window and create it with a command.

       Now I create a new TEST folder on the desktop through the command line (you can also create this folder anywhere else), and enter this folder

                                          

        

       Step 2: Turn this folder into a Git manageable repository by commanding git init

       

       At this time, you will find that there is a .git folder in TEST, which is used by Git to track and manage the repository. If you can't see it because it is hidden by default, then you need to set it to make hidden files visible.

       

       Step 3: At this point, you can paste your project into this local Git repository (you can view your current status through git status after pasting), and then add the project to the repository through git add (or git add . Add all files in this directory to the repository, note that the points are separated by spaces). You can actually always use git status to check your current status during this process .

       

       

       Here you are prompted that although the project has been pasted, it has not been added to the Git repository. Then we add all the copied projects to the repository through git add .

       

       

        Step 4: Submit the project to the repository with git commit .

        

        The quotation marks after -m are the comment content of this submission. This can be omitted, but it is better to write it, otherwise an error will be reported, and the details will be Googled by yourself.  Well, the work on our local Git repository is done, and now it's time to connect to the remote repository (that is, connect to Github)

      Since the transmission between the local Git repository and the Github repository is encrypted through SSH, you need to set the following when connecting:

      Step 5: Create SSH KEY. First check if there is a .ssh directory in the user directory of your C drive. If so, see if there are two files, id_rsa and id_rsa.pub . If so, skip to the next step. If not, create it with the following command

   $ ssh-keygen -t rsa -C "[email protected]"

       Then drive all the way back. Then you will find the two files    id_rsa and id_rsa.pub in the .ssh directory under the user

       

      Step 6: Log in to Github, find the icon in the upper right corner, open and click Settings, select SSH and GPG KEYS in it, click New SSH key in the upper right corner, then fill in the Title, and then put the id_rsa.pub just now. Copy the content to the Key content box under the Title, and finally click Add SSH key to complete the encryption of the SSH Key. The specific steps can also be seen below:

       


       

    

        Step 7: Create a Git repository on Github.

     You can directly click New repository to create it. For example, I created a TEST2 repository (because I already have a test repository in it, so I cannot create a TEST repository).

   

        Step 8: After creating the Git repository on Github, we can associate it with the local repository. According to the prompts on the created Git repository page, you can enter on the command line of the local TEST repository:

$ git remote add origin https://github.com/guyibang/TEST2.git

        

        Note that the address of the repository you created on Github is added after origin.

        

      Step 9: After the association is complete, we can push all the contents of the local library to the remote warehouse (that is, Github), through:

$ git push -u origin master

       Since the newly created remote repository is empty, you need to add the -u parameter. After the remote repository has content, the next time you upload content from the local repository, you only need to do the following:

$ git push origin master

        The process of uploading the project may take a while, and it looks like this after completion:

        

        At this time, if you refresh your Github page and enter the newly created repository, you will find that the project has been successfully uploaded:

      

        At this point, the entire process of uploading a local project to Github has been completed.

      In addition, there is a pit to pay attention to here, that is, when creating a remote repository in the seventh step above, if you checked Initialize this repository with a README (that is, a README file is automatically created for you when you create a repository), then it's time. In the ninth step, when you push the contents of the local repository to the remote repository, you will report a failed to push some refs  to  https://github.com/guyibang/TEST2.git error.

      

      This is because the README file in the newly created warehouse is not in the local warehouse directory. At this time, we can use the following command to merge the contents as follows:

$ git pull --rebase origin master

       

       Then you can push again and it will be successful.


     Summary: In fact, you only need to perform the following steps to upload your local project to Github

     1. Create a repository (ie folder) locally and turn it into a Git repository through git init ;

     2. Copy the project to this folder, and then add the project to the warehouse through git add .

     3. Then submit the project to the warehouse through git commit -m "comment content" ;

     4. After setting the SSH key on Github, create a new remote warehouse and associate the local warehouse with the remote warehouse through git remote add origin https://github.com/guyibang/TEST2.git ;

     5. Finally, push the project of the local warehouse to the remote warehouse (that is, Github) through git push -u origin master ; (if the README file is automatically created when creating a new remote warehouse, an error will be reported, see the solution above).




3. Git commands
View , add, submit, delete, retrieve, reset modified files

git help <command> # 显示commandhelp  

git show # 显示某次提交的内容 git show $id  

git co -- <file> # 抛弃工作区修改  

git co . # 抛弃工作区修改  

git add <file> # 将工作文件修改提交到本地暂存区  

git add . # 将所有修改过的工作文件提交暂存区  

git rm <file> # 从版本库中删除文件  

git rm <file> --cached # 从版本库中删除文件,但不删除文件  

git reset <file> # 从暂存区恢复到工作文件  

git reset -- . # 从暂存区恢复到工作文件  

git reset --hard # 恢复最近一次提交过的状态,即放弃上次提交后的所有本次修改  

git ci <file> git ci . git ci -a # 将git add, git rm和git ci等操作都合并在一起做                                    git ci -am "some comments"  

git ci --amend # 修改最后一次提交记录  

git revert <$id> # 恢复某次提交的状态,恢复动作本身也创建次提交对象  

git revert HEAD # 恢复最后一次提交的状态  

view file diff

git help <command> # 显示commandhelp  

git show # 显示某次提交的内容 git show $id  

git co -- <file> # 抛弃工作区修改  

git co . # 抛弃工作区修改  

git add <file> # 将工作文件修改提交到本地暂存区  

git add . # 将所有修改过的工作文件提交暂存区  

git rm <file> # 从版本库中删除文件  

git rm <file> --cached # 从版本库中删除文件,但不删除文件  

git reset <file> # 从暂存区恢复到工作文件  

git reset -- . # 从暂存区恢复到工作文件  

git reset --hard # 恢复最近一次提交过的状态,即放弃上次提交后的所有本次修改  

git ci <file> git ci . git ci -a # 将git add, git rm和git ci等操作都合并在一起做                                    git ci -am "some comments"  

git ci --amend # 修改最后一次提交记录  

git revert <$id> # 恢复某次提交的状态,恢复动作本身也创建次提交对象  

git revert HEAD # 恢复最后一次提交的状态  

View commit history

git log git log <file> # 查看该文件每次提交记录  

git log -p <file> # 查看每次详细修改内容的diff  

git log -p -2 # 查看最近两次详细修改内容的diff  

git log --stat #查看提交统计信息  

tig

Mac上可以使用tig代替diff和log,brew install tig

Git local branch management
View , switch, create and delete branches

git br -r # 查看远程分支  

git br <new_branch> # 创建新的分支  

git br -v # 查看各个分支最后提交信息  

git br --merged # 查看已经被合并到当前分支的分支  

git br --no-merged # 查看尚未被合并到当前分支的分支  

git co <branch> # 切换到某个分支  

git co -b <new_branch> # 创建新的分支,并且切换过去  

git co -b <new_branch> <branch> # 基于branch创建新的new_branch  

git co $id # 把某次历史提交记录checkout出来,但无分支信息,切换到其他分支会自动删除  

git co $id -b <new_branch> # 把某次历史提交记录checkout出来,创建成一个分支  

git br -d <branch> # 删除某个分支  

git br -D <branch> # 强制删除某个分支 (未被合并的分支被删除的时候需要强制)  

Branch merge and reba

git merge <branch> # 将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 (convenient for development and synchronization on multiple machines)

git merge <branch> # 将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 staging pipe

git stash # 暂存  

git stash list # 列所有stash  

git stash apply # 恢复暂存的内容  

git stash drop # 删除暂存区  

Git remote branch management

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 origin master # 将本地主分支推到远程主分支  

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

git remote -v # 查看远程服务器地址和仓库名称  

git remote show origin # 查看远程服务器仓库状态  

git remote add origin git@ github:robbin/robbin_site.git # 添加远程仓库地址  

git remote set-url origin git@ github.com:robbin/robbin_site.git # 设置远程仓库地址(用于修改远程仓库地址) git remote rm <repository> # 删除远程仓库 

Create a remote repository

git clone --bare robbin_site robbin_site.git # 用带版本的项目创建纯版本仓库  

scp -r my_project.git git@ git.csdn.net:~ # 将纯仓库上传到服务器上  

mkdir robbin_site.git && cd robbin_site.git && git --bare init # 在服务器创建纯仓库  

git remote add origin git@ github.com:robbin/robbin_site.git # 设置远程仓库地址  

git push -u origin master # 客户端首次提交  

git push -u origin develop # 首次将本地develop分支提交到远程develop分支,并且track  

git 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  





The method is transferred from: http://www.cnblogs.com/cxk1995/p/5800196.html

The second method is transferred from: http://blog.csdn.net/zamamiro/article/details/70172900

        </div>
            </div>


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324501794&siteId=291194637