I have a 'FeatureSubmission' branch, where I am suppose to add and submit latest feature for my repo.
--- Feature 2
|
---- master |
\ |
\..........................> FeatureSubmission.
\
\
\--- Feature 1
I am having trouble when navigating b/w these branches.
e.g. After the following sequence of events,
git branch Feature1 FeatureSubmission
git checkout Feature1
# .. add some new files
git add .
git commit -m 'Feature1'
git push origin Feature1
Now, Before starting to work on 'Feature2', I switched back to the submission branch
git checkout FeatureSubmission, And I get,
# git status
On branch FeatureSubmission
Your branch is up to date with 'origin/FeatureSubmission'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
./
nothing added to commit but untracked files present (use "git add" to track)
What Am I doing wrong ?
I made changes and pushed them to Feature1, why do they appear in 'FeatureSubmission' branch as 'Untracked' files ?
What is the proper way to navigate b/w branches i.e. FeatureSubmission Feature1 in such scenario ?
It seems like you're experiencing a common misunderstanding with Git's branch navigation and how untracked files behave across branches. Let's address your concerns and outline the correct approach to managing your workflow:
Understanding Untracked Files
Untracked Files: These are files in your working directory that were not in your last snapshot (commit) and are not in your staging area. Git sees them but has not been instructed to track them yet. Importantly, untracked files are not branch-specific. If you create a new file in one branch and then switch to another branch without committing it, the file will still be present in the working directory as an untracked file.
Why Untracked Files Appear After Branch Switch
When you switch from Feature1 to FeatureSubmission, the untracked files remain in your working directory because Git doesn't automatically delete or hide them when switching branches. This is because these files are not being tracked by Git in any branch yet.
Proper Workflow for Managing Branches and Changes
Commit or Stash Before Switching Branches: Before you switch branches, ensure that all changes are either committed or stashed. This practice keeps your working directory clean and prevents confusion with untracked files across branches.
Using Stash: If you have changes that you're not ready to commit before switching branches, you can use the git stash command to temporarily shelve changes. You can later reapply these changes with git stash pop or git stash apply after switching back.
git checkout FeatureSubmission
Do work in FeatureSubmission
git checkout Feature1 git stash pop
Clean Working Directory: Before switching branches, it's a good practice to have a clean working directory. You can check for untracked files and either decide to track them with git add and commit them or remove/delete them if they are not needed.
Branch Navigation: When you're done with changes in one branch and have committed them, you can switch to another branch using git checkout branch_name (or git switch branch_name for Git versions 2.23 and above).
Work and then commit any changes
git checkout Feature1 # Switch back to Feature1 Keeping Branches Updated: If you want to incorporate changes from FeatureSubmission into Feature1, you can merge or rebase:
Merging FeatureSubmission into Feature1:
git merge FeatureSubmission
Rebasing Feature1 on top of FeatureSubmission (for a cleaner history):
git rebase FeatureSubmission
To avoid issues with untracked files when switching branches, always ensure your working directory is clean—either by committing the changes, stashing them, or removing the untracked files. This approach will help you maintain a clear separation of changes between branches and avoid confusion.