How to switch back to 'master' with git?

Git

Git Problem Overview


I have made my first commit; then created a branch (let's say branch1).

In this branch I've created a directory 'example' and commited. In GitHub I see my new branch and the new directory 'example' that I have added.

Now I wonder how can I 'sync' back to master; and so have the 'example' folder deleted (as it doesn't exist on master).

EDIT : find . -type d -empty -exec touch {}/.gitignore \; did the job.

Git Solutions


Solution 1 - Git

You need to checkout the branch:

git checkout master

or

git checkout main

See the Git cheat sheets for more information.

Edit: Please note that git does not manage empty directories, so you'll have to manage them yourself. If your directory is empty, just remove it directly.

Solution 2 - Git

According to the Git Cheatsheet you have to create the branch first

git branch [branchName]

and then

git checkout [branchName]

Solution 3 - Git

Will take you to the master branch.

git checkout master

To switch to other branches do (ignore the square brackets, it's just for emphasis purposes)

git checkout [the name of the branch you want to switch to]

To create a new branch use the -b like this (ignore the square brackets, it's just for emphasis purposes)

git checkout -b [the name of the branch you want to create]

Solution 4 - Git

For deleting the branch you have to stash the changes made on the branch or you need to commit the changes you made on the branch. Follow the below steps if you made any changes in the current branch.

  1. git stash or git commit -m "XXX"
  2. git checkout master
  3. git branch -D merchantApi

Note: Above steps will delete the branch locally.

Solution 5 - Git

I'm trying to sort of get my head around what's going on over there. Is there anything IN your "example" folder? Git doesn't track empty folders.

If you branched and switched to your new branch then made a new folder and left it empty, and then did "git commit -a", you wouldn't get that new folder in the commit.

Which means it's untracked, which means checking out a different branch wouldn't remove it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDiscoView Question on Stackoverflow
Solution 1 - GitMatthew FarwellView Answer on Stackoverflow
Solution 2 - Gitblacksta500View Answer on Stackoverflow
Solution 3 - GitDapo MomoduView Answer on Stackoverflow
Solution 4 - GitSachin SridharView Answer on Stackoverflow
Solution 5 - GitDan RayView Answer on Stackoverflow