179

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.

0

5 Answers 5

305

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.

6
  • "Already on master" but my 'example' directory is still there. I want to get in sync with master (thus not having this folder as it doesn't exist in the master branch)
    – Disco
    Sep 14, 2011 at 13:38
  • # On branch master nothing to commit (working directory clean)
    – Disco
    Sep 14, 2011 at 13:43
  • @Disco: I've had this issue before-- if it's definitely not tracked in the index, you can just remove the directory locally and you'll get it back when you git checkout branch1. Sep 14, 2011 at 13:46
  • 1
    Mmmh too bad; isn't there an option for that ? I can't keep track of very directory in my head
    – Disco
    Sep 14, 2011 at 13:52
  • on github and gitlab (at least), you can add an empty file .gitkeep to any folder that is empty but should still be in the repo.
    – philshem
    Nov 3, 2020 at 20:11
8

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

git branch [branchName]

and then

git checkout [branchName]
0
6

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]

0
4

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.

1

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.

4
  • git init'ed my directory (/home/dev) then i did "commit -a" and pushed to github. Then i created a new branch; create a directory 'example' with some files inside and commited the branch. Now i want to get back to the initial stage (master) without the 'example' directory that i've create in the new branch.
    – Disco
    Sep 14, 2011 at 13:45
  • also in the master branch the directory 'example' is not there (as i see in github)
    – Disco
    Sep 14, 2011 at 13:47
  • update: i had one empty folder inside the example directory; is this why it still shows me the directory ? How to get rid of it using git
    – Disco
    Sep 14, 2011 at 13:50
  • You don't. git doesn't know about it. Just delete it. Checking out master should remove any files that are in the branch but not in master from your working directory.
    – Dan Ray
    Sep 14, 2011 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.