-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWikihow for Git.txt
153 lines (120 loc) · 5.7 KB
/
Wikihow for Git.txt
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
For a new project, initialize git
git init #Make sure Git is installed on your local machine
Then add the remote repository to the project
git remote add origin <https:github.linkToYourRemoteProject> or the SSH
And add the branch
git branch -M main #This makes it the default branch
Then add (all) the files to the repository
to add the file:
git add .
git add <nameoffile>
To add all the files
git add -A
After adding, commit with a message
git commit -m "initial commit" or sumn like that
Then push to GitHub
git push -u origin <nameOfBranch> #main by default
#############################################
FOR PROJECT THAT IS ALREADY ON GIT:
First add to git hub
git add -A
then commit with a message
git commit -m "message"
finally push to the branch
git push -u origin <nameOfBranch>
IMPORTANT NOTE
When you create a new empty repo on github and it already has a readme, LICENSE or any other file on creation and you try to push by
git push -u origin <nameofbranch>
Github not permit it likely because the remote repo has a README (therefore it is ahead) while the local does not.
You will likely see something like:
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/<user>/<reponame>.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
The solution is in the response.
Github suggests that you pull from the remote branch so everything will be up to date.
You pull by entering:
git pull origin <nameofremotebranch>
Again, this will likely throw an exception and it's likely to be the 'unrelated history' one.
* branch main -> FETCH_HEAD
* [new branch] main -> origin/main
fatal: refusing to merge unrelated histories
It's simple. The local and remote repos have a difference of a README file and since there hasn't been a push
to the remote, GitHub says these two repos don't have any history and won't push to the remote branch.
To fix this, we enter a simple line into the terminal:
git pull origin <nameofremotebranch> --allow-unrelated-histories
Abracadabra.We can now push successfully to the remote repository
i.e
git push -u origin <nameofbranch>
-u means upstream (as in remote)
TO CREATE A BRANCH
git branch <nameofnewbranch>
When you create a new branch, it will copy all the files from the default branch and then you can make whatever changes you want to in that branch
add the newly modified file:
git add <newlymodifiedfile>
commit:
git commit -m "commit message"
and push to the remote repository:
git push -u origin <nameofnewbranch>
GitHub will create the branch automatically remotely
TO VIEW all branches in the (local) repository
git branch
the branch that you're currently in will be highlighted in the terminal
TO ENTER A BRANCH
git checkout <nameofbranch>
** HANDLING CONFLICT **
If for instance, another contributor makes changes to a file and commits and one is working on the same file locally
OR
If a file is updated on the remote repository,
both of these two scenarios lead to CONFLICT and it not possible to push changes from the local to the remote because of the CONFLICT
For instance, this is file1.txt:
1. Line 1
Dev A adds a line to file1.txt and commits
1. Line 1
2. Modified by Remote dev
As fate would have it, we are similarly working on this file and we're done with the changes and ready to push to Git
1. Line 1
2. Modified by Local dev
We do:
git add "file1.txt"
git commit -m "changes made locally"
and then when i try:
git push -u origin <remotebranch>
hell breaks loose in the terminal:
To github.com:<user>/<remote_repo_name>.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'github.com:<user>/<remote_repo_name>.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
This happens because of CONFLICT!
The solution is in the hint once again.
The remote and local repository have to be in sync and in order to do this, we call an old trick from the magic book:
git fetch
git merge #combines the remote and local repo at the stage to show the conflict
file1.txt now looks like this after merging:
1. Line 1
<<<<<< HEAD (Current Change)
2. Modified by Local dev
====
3. Modified by Remote dev
>>>>>> refs/remotes/origin/<name_of_remote_branch> (Incoming Change)
We now have to fix the conflict manually, we could decide to:
-Accept current change (Local change overwrites remote change)
-Accept incoming change (Remote change overwrites local change) or
-Accept both changes (both changes stay alive)
If we decide to resolve the conflict by ACCEPTING BOTH CHANGES:
file1.txt now looks like this:
1. Line 1
2. Modified by Local dev
3. Modified by Remote dev
after resloving the conflict, we now add, commit and push upstream:
git add "file1.txt"
git commit -m "resolve conflict"
git push -u origin <name_of_remote_branch>
This should work without hitches and we can now recline in a chair and glare at the setting sun in the horizon.