More notes on Git
Last updated on 22 Dec. 2024
Older notes:
- Notes: Git & GitHub: In project with main branch deployed to hosting site, using newfeature git branch for development and eventual merging into main branch, https://raviswdev.blogspot.com/2024/06/notes-git-github-in-project-with-main.html
- Solving syncing issue with local git (branch master) after choosing license while making new public repo on Github (branch main); More git trials, https://raviswdev.blogspot.com/2024/04/solving-syncing-issue-with-local-git.html
- Git section in Learning Full Stack (MERN) Web App Development through Free Online Tutorials – Organized Notes and Log, https://raviswdev.blogspot.com/2024/03/learning-web-app-development-through.html#git
-------
To clone only a specific branch:
git clone -b <branchname> --single-branch <remote-repo-url>
---
To check if remote is ahead of local branch, I think I need to do following:
Run 'git remote update'
Then run 'git status' (or 'git status -uno')
Ref: Check if pull needed in Git, https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
-----
==================
git pull error if package-lock.json gets updated by npm install for project which has been earlier cloned or pulled from GitHub but on which no development changes have been made:
error: Your local changes to the following files would be overwritten by merge:
package-lock.json
Please commit your changes or stash them before you merge.
Aborting
---
One solution that works for me: Discarding all changes made to packagelock.json and then running 'git pull' again.
Then one can run npm install (which I think would install only required packages, if any), followed by npm start to run the app.
======================
24 Nov. 2024:
VSCode cloning of a repo gets all branches of the repo but to switch to the non-default branch one has to use:
git checkout branch-name [e.g.: git checkout finished-files]
It shows output like:
Switched to a new branch 'finished-files'
branch 'finished-files' set up to track 'origin/finished-files'.
---
[For repo: https://github.com/nikitapryymak/formik-tutorial having two branches of starting-files and finished-files :]
Interestingly, immediately after cloning, following command:
git branch -a
gave output of:
* starting-files
remotes/origin/HEAD -> origin/starting-files
remotes/origin/finished-files
remotes/origin/starting-files
----
So a local finished-files branch had not been created.
But after I used 'git checkout finished-files' and then ran 'git branch -a' again, I got output:
* finished-files
starting-files
remotes/origin/HEAD -> origin/starting-files
remotes/origin/finished-files
remotes/origin/starting-files
----
And I could see files of finished-files branch in VSCode.
----
By mistake in an earlier attempt, immediately after cloning, I had used:
git branch branch-name
followed by:
git checkout branch-name
That seems to have tripped up the process as now I had created a new branch of name: branch-name. So I was shown the same code as earlier branch!
--------
Only master branch is visible after cloning a Git repo, https://stackoverflow.com/questions/8889753/only-master-branch-is-visible-after-cloning-a-git-repo , 2012 :
When you clone a repository, all remote branches are created as "remote tracking branches" in your repository. These aren't shown by default, but you can see these with:
git branch -a
If you do a git checkout new-branch, git will find the remote tracking branch of the same name, automatically create a new local branch from the same commit, and switch to the new local branch.
=====
The above info. seems to match my successful attempt. The trip-up point is making a local branch with same name before issuing git checkout. With this knowledge, I opened my earlier unsuccessful project in VSCode (I had saved it with a new name), deleted the finished-files (local) branch, and then did git checkout finished-files which worked like in the successful attempt. So now even in the earlier-unsuccessful project code, I could view remote finished-files branch code! The commands and the output are given below:
PS D:\Users\user-name\Projects\VSCode\React\formik-yup\formik-tutorial-v1> git branch -a
finished-files
* starting-files
remotes/origin/HEAD -> origin/starting-files
remotes/origin/finished-files
remotes/origin/starting-files
PS D:\Users\user-name\Projects\VSCode\React\formik-yup\formik-tutorial-v1> git checkout finished-files
Switched to branch 'finished-files'
PS D:\Users\user-name\Projects\VSCode\React\formik-yup\formik-tutorial-v1> git checkout starting-files
Switched to branch 'starting-files'
Your branch is up to date with 'origin/starting-files'.
PS D:\Users\user-name\Projects\VSCode\React\formik-yup\formik-tutorial-v1> git branch -d finished-files
Deleted branch finished-files (was bfd383c).
PS D:\Users\user-name\Projects\VSCode\React\formik-yup\formik-tutorial-v1> git checkout finished-files
Switched to a new branch 'finished-files'
branch 'finished-files' set up to track 'origin/finished-files'.
PS D:\Users\user-name\Projects\VSCode\React\formik-yup\formik-tutorial-v1> git branch -a
* finished-files
starting-files
remotes/origin/HEAD -> origin/starting-files
remotes/origin/finished-files
remotes/origin/starting-files
PS D:\Users\user-name\Projects\VSCode\React\formik-yup\formik-tutorial-v1> git checkout starting-files
Switched to branch 'starting-files'
Your branch is up to date with 'origin/starting-files'.
PS D:\Users\user-name\Projects\VSCode\React\formik-yup\formik-tutorial-v1> git checkout finished-files
Switched to branch 'finished-files'
Your branch is up to date with 'origin/finished-files'.
==========
An easy way to differentiate between wanted remote branch 'finished-files' from wrongly created local branch 'finished-files' is to look at the commit id (using git log). If the local branch was wrongly created its commit id will not match the remote's commit id (and may match commit id of another branch, as was the case for me). If the local branch is correctly created the commit id will match with remote commit id.
===============
Git: how to get all the files changed and new files in a folder or zip?, https://stackoverflow.com/questions/4126300/git-how-to-get-all-the-files-changed-and-new-files-in-a-folder-or-zip
zip is not supported on my PC. After some trials, got the Powershell command for my PC
& "C:\Program Files\7-Zip\7z.exe" a modified-files.zip $(git ls-files --modified)
Added above command as PS script - GitModFilesZip.ps1
======================
Git push new local branch to remote, https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/git-push-new-branch-remote-github-gitlab-upstream-example : "Unfortunately, new Git branches don’t automatically push to a remote Git repo like GitHub or GitLab." Ravi: But that's what I was looking for!
Git Tools - Stashing and Cleaning, https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning : "Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time"
Comments
Post a Comment