February 1, 2023

Git push to GitHub error solution

前言

今天想把自己网站的源代码上传到GitHub仓库里保存,这样之后也可以在多台电脑上操作了。但用Git push到GitHub时出错了,我想应该是之前把这个源代码在不同的电脑上到处拷贝操作上传导致版本信息不一致导致的。所以有了如下操作及解决过程。


过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Last login: Wed Feb  1 13:57:43 on console
andy@Andy-iMacPro ~ % cd Repo/MyBlog

andy@Andy-iMacPro MyBlog % git add .

andy@Andy-iMacPro MyBlog % git commit -m "jinchuan site source code"
[main 44b58ad] jinchuan site source code
3 files changed, 136 insertions(+), 17 deletions(-)
create mode 100644 source/_posts/RaspberryPi-Fan-Control-Based-on-Temperature.md

andy@Andy-iMacPro MyBlog % git branch -M main

andy@Andy-iMacPro MyBlog % git remote add origin https://github.com/JinchuanL/jinchuan.site.git
error: remote origin already exists.

andy@Andy-iMacPro MyBlog % git push -u origin main
To https://github.com/JinchuanL/jinchuan.site.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/JinchuanL/jinchuan.site.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

andy@Andy-iMacPro MyBlog % 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=origin/<branch> main

andy@Andy-iMacPro MyBlog % git pull https://github.com/JinchuanL/jinchuan.site.git main
From https://github.com/JinchuanL/jinchuan.site
* branch main -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

andy@Andy-iMacPro MyBlog % git pull --ff-only https://github.com/JinchuanL/jinchuan.site.git main
From https://github.com/JinchuanL/jinchuan.site
* branch main -> FETCH_HEAD
fatal: Not possible to fast-forward, aborting.

andy@Andy-iMacPro MyBlog % git rebase main
Current branch main is up to date.

andy@Andy-iMacPro MyBlog % git rebase https://github.com/JinchuanL/jinchuan.site.git main
fatal: invalid upstream 'https://github.com/JinchuanL/jinchuan.site.git'

andy@Andy-iMacPro MyBlog % git rebase https://github.com/JinchuanL/jinchuan.site.git
fatal: invalid upstream 'https://github.com/JinchuanL/jinchuan.site.git'

andy@Andy-iMacPro MyBlog % git rebase main
Current branch main is up to date.

andy@Andy-iMacPro MyBlog % git pull https://github.com/JinchuanL/jinchuan.site.git main
From https://github.com/JinchuanL/jinchuan.site
* branch main -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

andy@Andy-iMacPro MyBlog % git pull.rebase true https://github.com/JinchuanL/jinchuan.site.git main
git: 'pull.rebase' is not a git command. See 'git --help'.

andy@Andy-iMacPro MyBlog % git config pull.rebase true

andy@Andy-iMacPro MyBlog % git pull https://github.com/JinchuanL/jinchuan.site.git main
From https://github.com/JinchuanL/jinchuan.site
* branch main -> FETCH_HEAD
warning: unable to rmdir 'themes/hexo-theme-cupertino': Directory not empty
Successfully rebased and updated refs/heads/main.

andy@Andy-iMacPro MyBlog % git push -u origin main
Enumerating objects: 68, done.
Counting objects: 100% (68/68), done.
Delta compression using up to 12 threads
Compressing objects: 100% (52/52), done.
Writing objects: 100% (67/67), 1.76 MiB | 892.00 KiB/s, done.
Total 67 (delta 5), reused 0 (delta 0), pack-reused 0
^C

andy@Andy-iMacPro MyBlog % killall ssh-agent; eval `ssh-agent`
No matching processes belonging to you were found
Agent pid 1347

andy@Andy-iMacPro MyBlog % git push -u origin main
Enumerating objects: 68, done.
Counting objects: 100% (68/68), done.
Delta compression using up to 12 threads
Compressing objects: 100% (52/52), done.
Writing objects: 100% (67/67), 1.76 MiB | 2.76 MiB/s, done.
Total 67 (delta 5), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (5/5), done.
To https://github.com/JinchuanL/jinchuan.site.git
bd20a3d..5259aad main -> main
branch 'main' set up to track 'origin/main'.

andy@Andy-iMacPro MyBlog % git config pull.rebase false

andy@Andy-iMacPro MyBlog % git pull https://github.com/JinchuanL/jinchuan.site.git main
From https://github.com/JinchuanL/jinchuan.site
* branch main -> FETCH_HEAD
Already up to date.

andy@Andy-iMacPro MyBlog % git push -u origin main
branch 'main' set up to track 'origin/main'.
Everything up-to-date

解决思路




总结

换电脑或者版本信息不一致时,建议走如下流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# main: my branch, replace main or switch to others based your situation
git rebase main

# Set rebase to true when do a pull request, by default it is false
git config pull.rebase true

# Try pull request again
# git pull <source> <branch>
git pull https://github.com/JinchuanL/jinchuan.site.git main
$Successfully rebased and updated refs/heads/main. # Looks all good

# Then we can push our local repo to GitHub repo now.
git push -u origin main

# Personally, I like to put things back to it's original place
# So one more step, put pull.rebase back to false so next time when we do a pull request, it won't rebase again
git config pull.rebase false

# Check
git config pull.rebase
$false # All good, already change back to origin place

# If Terminal stucks when trying to push to GitHub
# Restart your ssh agent!
killall ssh-agent; eval `ssh-agent`
# And try to push again


Reference

About this Post

This post is written by Andy, licensed under CC BY-NC 4.0.

#TroubleShooting#Git