Solving syncing issue with local git (branch master) after choosing license while making new public repo on Github (branch main); More git trials
Last updated on 1 May 2024
Today I tripped up on my standard routine (as captured in this post of mine) of creating a local git repository, followed by creating a public repository on Github and then pushing contents of local git repo to remote Github repo.
The trip-up started with me opting to choose a license file in Github as I was creating a new public repo there. That resulted in Github doing an initial commit to the repo with the license file. But now I was not shown the git commands to “…or push an existing repository from the command line” on Github! I could not locate where these commands could be seen on Github.
24 Apr. 2024 Update:
This post of mine lists the git commands to “…or push an existing repository from the command line” on Github that I had used for one project. These commands were:
- git remote add origin https://github.com/ravisiyer/react_deploy_gh.git
- git branch -M main
- git push -u origin main
I think the 2nd command simply renames the local git branch to main (from master). So, once that is executed, both the local git repo and remote Github repo will use main branch and there will be no master branch on local git repo thus avoiding confusion.
end-24 Apr. 2024 Update
Then I searched the net for these commands. IFIRC I followed the commands listed here. Specifically, IFIRC, I executed the following git commands on my PC:
- git remote add origin https://github.com/ravisiyer/flex-justify-center-cut-off.git
- git push -u -f origin master
The first command went through without error. But the git push command gave some errors. When I checked on Github, I saw that now there were two branches: main with what Github had created (license file) and master having files of of my project on local PC. I do not know enough about git branches to handle merging such branches.
I did some more net searching and came across this very interesting article: Of Git and GitHub, Master and Main. I was glad to see somebody write about this different branch names of main and master issue between git and Github. I get the impression that the standard routine I had followed earlier automatically resolved this issue.
I was not clear about what to do next for this project. Of course, I could simply delete the public Github repo and redo it but this time follow the standard routine (not create license as part of Github repo creation). But I decided to explore other ways to do it as a way to increase my knowledge about git and Github.
After some exploration, this seemed to work:
- Removing the master branch on Github repo using Github UI
- Changing git branch to main using command: git branch -m main
- Breaking the local and remote connection on git using the command: git remote remove origin [As the command completed without a message, to check that it removed remote origin repo connection, I then ran: 'git remote show origin' which gave an (expected) error that origin does not appear to be a git repo.]
- Then creating a remote connection to the Github repo using: git remote add origin https://github.com/ravisiyer/flex-justify-center-cut-off.git
- Finally pushing local git repo to Github remote using: git push -u -f origin main (this gave the message: "To https://github.com/ravisiyer/flex-justify-center-cut-off.git", " + 4146248...473b829 main -> main (forced update)", "branch 'main' set up to track 'origin/main'").
After the above commands were done, Github repo showed the local project files (under main branch) but the license file that was there earlier on Github repo main branch was gone! Perhaps the "forced update" part resulted in git local repo contents replacing git remote repo contents.
To try out some more unusual stuff, I then added a LICENSE file on Github and also a README.md file. As I had seen earlier, git status did not report that remote Github repo (main branch) had these changes - I don't know enough of git to know whether this is normal or not. [Update: 'git remote show origin' command shows '(local out of date)' in such situations. Details provided in another section below.] But on executing 'git pull' on local PC, the LICENSE file and README.md file from Github repo got pulled into the local PC project too. Now the two repos (main branch) were in sync. I modified README.md slightly on local PC (using VSCode) and did the usual routine of 'git add .', 'git commit -m xxx' and 'git push' on local PC. This pushed the changes to Github repo.
So now the local and remote repos seem to be in sync and the usual git commands are resulting in expected outcomes. I was able to successfully deploy the app. on Github pages (link removed as later I had to archive the Github repo).
I later changed the Github repo url using Github UI to https://github.com/ravisiyer/flex-justify-center-cut-off-and-fix to reflect that I had provided a fix page too. That, IFIRC, resulted in the first git push to work but provide a warning, "remote: This repository moved. Please use the new location:
remote: https://github.com/ravisiyer/flex-justify-center-cut-off-and-fix.git".
However the next push failed with an error. Don't have the message now but IFIRC it said that flex-justify-center-cut-off.git repo was not found. I thought that the fix was to run following command:
git remote set-url origin https://github.com/ravisiyer/flex-justify-center-cut-off-and-fix
To check whether above command worked, I used the command:
git remote get-url origin
which gave output of:
https://github.com/ravisiyer/flex-justify-center-cut-off-and-fix
But git push failed again this time with a connection reset error.
To continue my learning on git and Github, I then tried out the following:
- Copied the source folder on local PC to a backup folder
- Opened source folder in VSCode
- Used 'git remote remove origin' to break local git repo connection with remote repo.
- Created new public repo on Github named "flex-center-clip" but this time not choosing option to create license or README file (I had also changed my local PC top-level folder name to "flex-center-clip").
- This time after repo creation, Github showed me the 'Quick setup' info. with "…or push an existing repository from the command line" listing the following commands:
- git remote add origin https://github.com/ravisiyer/flex-center-clip.git
- git branch -M main
- git push -u origin main
-
I executed the above commands (shown in above step). It succeeded with
first two commands not showing any message and the third command (git
push) showing the message as follows:
Enumerating objects: 21, done.
Counting objects: 100% (21/21), done.
Delta compression using up to 4 threads
Compressing objects: 100% (21/21), done.
Writing objects: 100% (21/21), 93.07 KiB | 4.90 MiB/s, done.
Total 21 (delta 8), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (8/8), done.
To https://github.com/ravisiyer/flex-center-clip.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
- Now the Github repo had all the files of the local repo and also had the commit history (6 commits) of the local repo! That's great as I wanted this commit history to be put up on Github repo. Alternatively, I could have simply deleted the .git folder of local directory (so deleting local git repo), and then gone through the routine of 'git init' .... push to Github repo. But then I would have lost the commit history.
- I published the Github repo on Github pages: https://ravisiyer.github.io/flex-center-clip/ which seems to be working as expected.
A question now is whether I should delete the old Github repo: https://github.com/ravisiyer/flex-justify-center-cut-off-and-fix (and old local folder on PC). I probably should to avoid confusion. Github gives lot of warnings about deleting a repo. Perhaps there is an archive kind of alternative. Will explore that. ... I removed the github pages deployment. Then I made the repo private and then archived it (archive makes it readonly). Slightly cumbersome process but still doable.
...
An additional related article: https://www.digitalocean.com/community/tutorials/how-to-push-an-existing-project-to-github
...
When Github repo is ahead of local PC git repo, 'git status' output:
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
----
But 'git remote show origin' output is:
* remote origin
Fetch URL: https://github.com/ravisiyer/JSWriteOnNewWindow.git
Push URL: https://github.com/ravisiyer/JSWriteOnNewWindow.git
HEAD branch: main
Remote branch:
main tracked
Local branch configured for 'git pull':
main merges with remote main
Local ref configured for 'git push':
main pushes to main (local out of date)
----
So above command informs us that local is out of date!
On executing 'git pull' command, its output:
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), 2.72 KiB | 56.00 KiB/s, done.
From https://github.com/ravisiyer/JSWriteOnNewWindow
dd508aa..1c8344e main -> origin/main
Updating dd508aa..1c8344e
Fast-forward
LICENSE | 21 +++++++++++++++++++++
README.md | 3 +++
2 files changed, 24 insertions(+)
create mode 100644 LICENSE
create mode 100644 README.md
----
Now 'git remote show origin' command output is:
* remote origin
Fetch URL: https://github.com/ravisiyer/JSWriteOnNewWindow.git
Push URL: https://github.com/ravisiyer/JSWriteOnNewWindow.git
HEAD branch: main
Remote branch:
main tracked
Local branch configured for 'git pull':
main merges with remote main
Local ref configured for 'git push':
main pushes to main (up to date)
----
So instead of "(local out of date)", we get the message, "(up to date)" for "Local ref"
Comments
Post a Comment