More notes on Git

Last updated on 15 Jun 2025
Older notes:
  1. 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
  2. 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
  3.  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')
-----
==================

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!
--------

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.

===============

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"
===================

Role-Based Authentication in React (Complete Tutorial), https://www.youtube.com/watch?v=-IqMxPU3vbU , 26 mins. 30 secs., Aug. 2024.

Issue is that the the source code path is a folder in a large repo. We need to only download the above folder.

Clone a Specific Folder from a GitHub Repository, https://medium.com/@gabrielcruz_68416/clone-a-specific-folder-from-a-github-repository-f8949e7a02b4, Oct. 2023  has some instructions to do the above.

The steps (modified for above repo path) are:
1) git clone --filter=blob:none --no-checkout https://github.com/cosdensolutions/code.git
2) cd code (folder created by above with only .git folder)
3) git sparse-checkout set --cone
4) git checkout master 
5) git sparse-checkout set videos/long/role-based-authentication-in-react
Tried the above commands. They seem to have worked. It downloaded the files quite quickly. Less than a minute surely and more like 10 to 20 seconds, IIRC.
Size of code directory now is 1.91 MB with 69 Files, 26 Folders

The entire repo downloaded as a zip (code-master.zip) is 21,121 KB. Unzipping it now. It is taking time to unzip! 5 to 10 minutes!
Size of unzipped directory (code-master) is 74.0 MB with 4,265 Files, 697 Folders.

So the above procedure to clone only a specific folder is much better than downloading entire repo as zip, unzipping it and picking up the folder we need.

...
To download above path of repo (and not full repo), use:
git clone -n --depth=1 --filter=tree:0 https://github.com/MicrosoftEdge/Demos.git
cd Demos
git sparse-checkout set --no-cone /pwa-getting-started
git checkout
------
Ref: How to clone only some directories from a git repository?, https://askubuntu.com/questions/460885/how-to-clone-only-some-directories-from-a-git-repository

==================
June 2025

Gem on stash

What is git stash?
git stash temporarily saves changes that you don't want to commit immediately. It takes your modified tracked files and staged changes, saves them on a "stash" stack, and then reverts your working directory to match the HEAD commit. This is incredibly useful when you're in the middle of working on something, but need to switch branches, pull updates, or address a quick bug fix without committing half-finished work.

How to use git stash:
Stashing your changes:
Navigate to your project's root directory in your terminal (where your .git folder is located).

git stash
This command will save your current changes and clear your working directory. If you want to add a descriptive message to your stash (highly recommended, especially if you plan to have multiple stashes), use:

git stash save "Work in progress: Button UI changes and settings modal"
or simply:

git stash push -m "Work in progress: Button UI changes and settings modal"

Listing your stashes:
To see a list of your saved stashes:

git stash list
You'll see output like stash@{0}: On main: Work in progress: Button UI changes...

Applying your stashed changes:
When you're ready to bring your changes back, you have a few options:

git stash apply: This will reapply the most recently stashed changes to your working directory, but keep the stash in the stash list. This is useful if you might want to apply the same stash to multiple branches.
git stash pop: This will reapply the most recently stashed changes to your working directory, and then remove that stash from the stash list. This is often the most common use case if you just need to get your work back and you're done with the stash.
Applying a specific stash: If you have multiple stashes, you can apply a specific one by its identifier (e.g., stash@{1}):

git stash apply stash@{1}
or

git stash pop stash@{1}
Deleting a stash:
If you no longer need a particular stash (e.g., after popping it, or if you decided to abandon those changes), you can drop it:

git stash drop stash@{0}
To clear all stashes:

git stash clear
------
My usage of stash ...

TimestampTrackerWeb> git stash push -m "UI/UX button changes with settings button and color fixes"
warning: in the working copy of 'App.js', LF will be replaced by CRLF the next time Git touches it
Saved working directory and index state On main: UI/UX button changes with settings button and color fixes
[The App.js file changes seem to have gone from working version.]
... break from this work ...
TimestampTrackerWeb> git stash  list
stash@{0}: On main: UI/UX button changes with settings button and color fixes
TimestampTrackerWeb> git stash pop
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   App.js

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (d77a82b9307463e3be645f105d21095df17c4cf6)
[The App.js file changes have come back in working version.]
TimestampTrackerWeb> git stash list
TimestampTrackerWeb>
---

Comments