git notes (common commands, frequently asked questions)

git upload steps

Step 1: Right-click the folder to be uploaded, and select the Git Bash Here option in the right-click menu bar

Step 2: Enter git init in the pop-up Git command window (the .git file is generated at this time)

Step 3: Copy the HTTPS link of the gitee repository

Step 4: Enter git remote add origin+ the HTTPS link of the copied warehouse in the Git command window

 Step 5: Enter git pull origin master in the Git command window and enter the corresponding account and password of the code cloud login in the pop-up window (if you want to force push, you can directly git push -f origin master at this step (this command It is best not to use it when the team is developing, otherwise it may be life-threatening))

Step 6: Enter git add . (. means all files) or git add + file name (function: save the file to the cache area)

Step 7: Enter git commit -m 'file description' (write the content of the file description casually)

Step 8: Enter git push origin master to push the local warehouse to the remote warehouse (if you are not in the master branch, remember to replace master with the name of your branch)

Step 9: After completing the above operations, you can go to Code Cloud to view the results just uploaded

 git common commands

git init

git remote add origin+ the HTTPS link of the copied warehouse

git pull origin master

git add .

git commit -m 'file description'

git push origin master

git clean n //This is to clear the file preview

git clean -f //Forcibly clear files

git fetch

git checkout -a

git checkout origin/master

git status

Stepping on the pit collection

(The following are all blogs that I have used to solve problems myself. I think they are good blogs. I made them into a collection to facilitate my future search. The links to the original texts will be placed at the bottom .)

pit 1

An error occurred after git push![rejected] master -> master(non-fast-forward) error:failed to push some refs to XXX

insert image description here

I created a project locally
and created a warehouse on Code Cloud. I want to link the local warehouse to the remote warehouse.
I use the following method:
git init     to initialize the local warehouse.
git remote add origin XXX      to add the remote warehouse address.
If you are here Then execute
git add -A ,
git commit -m " "
git push origin master , then this problem will occur (rejected), so don't worry about git add after remote add, be sure to git pull origin master, this is the reason
It is because the warehouse you created in Code Cloud has a ReadMe file, but not locally, causing the local and remote to be out of sync.
Then there are two solutions :
one:
If there is no ReadMe file locally, then generate one locally:

git pull --rebase origin master      generate ReadMe file locally
git push origin master

Two:
Then I will force the upload to overwrite the remote file,
git push -f origin master
(this command is best not to be used when the team is developing, otherwise it may be life-threatening)

pit 2

The solution to (master|REBASE 1/1) after git rebase

 When the following error occurs when using git push without first using pull, first directly git add and git  commit


This error indicates that there is a conflict between your local code and the service code. You can check the local git log with git log and find that it is different from the service log. It means that someone has submitted the code before you, and the service log is different from the local log. The solution is

1. First use git pull --rebase to pull down the code of the server

git pull --rebase

Then you will see this effect:

 This shows that there is a conflict, we need to resolve the conflict first

2. After resolving the conflict, git add, and then git status to see if the submitted file is ready

At this time it will prompt

git rebase --abort  // 取消合并
git rebase --continue // 继续执行

3. After we are ready, we will continue to execute in git rebase --continue, so that the log will be merged.

4. Finally, we are submitting git push

pit 3

When git push to GitHub, I encountered! [rejected] master -> master (non-fast-forward) problem

The operation process of the manage project named

1. Open git in the manage folder, enter git init to initialize the local warehouse, and GitHub creates a remote warehouse manage

2. The following command associates the local and remote warehouses, ***** is my username

    git remote add origin [email protected]:******/manage.git

3. There is already project code locally. After add and commit, I want to push to the remote warehouse

     git push origin master

     At this time, an error is reported  :

     ! [rejected] master -> master (non-fast forward)    

      …………    

      …………

   After searching the Internet for a long time, after entering various invalid and wrong commands, I finally found a solution:

  1. git pull origin master --allow-unrelated-histories //Synchronize the remote warehouse with the local one to eliminate differences

  2. Re-add and commit the corresponding files

  3、git push origin master

  4. Now you can upload successfully

pit 4

Git使用之(error: pathspec 'master' did not match any file(s) known to git)

An overview of the problem

I encountered a problem at work today. I have used a local git repository for a long time, and there is only a development branch in it. Now I want to switch the branch to the master branch. The problem comes. When switching to the master branch:

git checkout master

Prompt the following error:

error: pathspec 'master' did not match any file(s) known to git

Two problem solving

1. First, let's look at the branch situation:

git branch -a
* develop
  remotes/composer/develop
  remotes/composer/feature/194
  remotes/composer/feature/198
  remotes/composer/feature/199
  remotes/composer/feature/200
  remotes/composer/master
  remotes/origin/HEAD -> origin/develop
  remotes/origin/develop
  remotes/origin/feature/194
  remotes/origin/feature/198
  remotes/origin/feature/199
  remotes/origin/feature/200
  remotes/origin/master

2. If you don't see the branch you want, get all the branches first:

git fetch

3. Switch to the remote master branch:

git checkout origin/master

Tips are as follows:

Note: checking out 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 4beea49... Merge branch 'develop' into 'master'

Execute git branch, the effect is as follows:

* (detached from origin/master)
  develop

5. Now we can switch from the current detached branch and create a new branch. It can be understood that the newly created branch is derived from the current detached branch (in order to prepare for the follow-up, the new branch here is called master):

git checkout -b master

5. At this time, we will prompt the following error when using git pull:

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=<remote>/<branch> master

It means that our newly created master branch cannot establish a tracking relationship with the remote master branch (although we seem to have established the master branch on the surface, but git does not think it has any relationship with the remote master), of course, you can follow the above tips In that way, update by specifying the remote branch and the local branch through git pull, but here we use the second method in the prompt to establish the tracking relationship between the local branch and the remote branch:

git branch -u origin/master master

6. At this time, we execute git pull to see what feedback:

Already up-to-date.

Summary: In fact, the humanization of git is very complete. Sometimes we should not be afraid of prompts, but find solutions to problems from them, and make good use of them from time to time:

man git
man git branch

and so forth!

Bye!

Pit 5 

Git-command-line-rescue "Your local changes to the following files would be overwritten by checkout"

accident scene

Sometimes, when we use  git checkout <branchname>the command to switch branches, sometimes the switch fails, and then the following prompt message appears:

Write picture description here

The prompt information is very clear,
the current branch has untracked files, and the checkout command will overwrite them, please cache (stash) or submit (  commit  ).

Let's talk about the solution first

At this point, you have two options:

###1. The content changes of untracked files are very important, save the changes

//第一种方式 存到暂存区
git add.
git stash 
//取出的时候使用 
git stash pop

//第二种方式 发起一个commit 存到提交历史
git add.
git commit -m "commit message"

###2. The content changes of untracked files are not important, give up the modification

There are two ways to do this, clear changes and force switch branches

Recommended practice: Clear untracked files

git clean n  //这个是清除文件预览
git clean -f //强制清除文件

Write picture description here

Force switch branch

The command to forcibly switch branches is as follows. As the prompt says, the untracked files will be overwritten directly. I think this method is very rude. When we switch daily, we should not use  -f forced switching. There is no overwriting prompt, and it is easy to lose file modifications, but we don’t know it.

git checkout -f <branch> 

Write picture description here

analyze the reasons

git's local version management has three parts

git's local version management has three parts

Name Description
Working Directory (Working Directory) The part of the file we directly edit
Staged Snapshot (Staged Snapshot) The place where the file is saved after executing git add
Commit History The place where the file is executed after git commit.
The three of them The relationship looks like this:

The picture comes from the Internet

(Explanation: The picture is not original, it comes from the graphic Git)

When we perform the checkout operation, git will check whether there are untracked files in the workspace, which is why we have an error message when we perform checkout above.
 

pit 6

you need to resolve your current index first

 After switching from one branch A to another branch B, perform a pull operation on the switched B branch, because the pull operation actually includes the fetch+merge operation. When performing the merge operation, the B branch has not been executed for a long time In the pull/merge operation, there is a big difference between the local B branch library and the B branch library in the remote (and these differences are files developed by other colleagues). When merging, there is a conflict, so that the status of the B branch is merging, which actually means The merge failed and remained in the merge state, and the pull operation could not be performed. At this time, the conflict was not resolved, but the checkout/switchto operation was performed from the B branch, and when trying to switch to other branches, it reported:
[plain] view plaincopy View code slices on CODE Derived to my code slice

    ....java: needs merge  
    …….java : needs merge  
    …….java : needs merge  
    error : you need to resolve your current index first  

’s mistake, after a long time of google, I finally found the answer on stackoverflow, the big question means: merge failed, there are conflicts If it is not resolved, you can:

1. Execute merge again after resolving the conflicts;

2. Go back to before merge

. Since the merge conflict is a file of another colleague, I don’t need to resolve conflicts, so go back to before merge and simply change me Push the file again, execute the following code:

    git reset --merge  

to get it done!

By the way, paste the link on stackoverflow:

http://stackoverflow.com/questions/6006737/git-merge-errors

pit 7

An Incorrect username or password ( access token ) prompt appears

insert image description here

Analysis: The reason for the appearance is that the account number and password for logging in to the wrong code cloud were entered in the pop-up window in the fifth step.

Solution: Go to Control Panel>User Management>Credential Manager to modify the Windows credentials (because I am a Win 10 system here, there may be differences)

insert image description here

pit 8

GE007: Your push would publish a private email address prompt appears

insert image description here

 Solution: Go to the upper right corner of the gitee personal homepage and select Settings>Multiple mailbox management>Cancel the check box that prohibits command line push from exposing personal mailboxes

insert image description here

 Original content link collection:



(119 messages) An error occurred after git push![rejected] master -> master(non-fast-forward) error:failed to push some refs to XXX_longfan's Blog-CSDN Blog

(119 messages) The solution to (master|REBASE 1/1) after git rebase_is a blog with a name-CSDN Blog

(119 messages) git push to GitHub encountered! [rejected] master -> master (non-fast-forward) problem_xieneng2004's blog-CSDN blog

 (119 messages) Used by Git (pathspec master did not match any file(s) known to git)

 (119 messages) Git-command line-rescue "Your local changes to the following files would be overwritten by checkout"_Duan Qianqian Blog-CSDN Blog

 (119 messages) you need to resolve your current index first solution_wenwenxiong's column-CSDN blog

 (119 messages) git upload steps_吴啊#'s blog-CSDN blog_git upload

Guess you like

Origin blog.csdn.net/weixin_51867622/article/details/123098488