176

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.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
Disco
  • 4,226
  • 11
  • 58
  • 76

5 Answers5

301

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.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • "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 '11 at 13:38
  • # On branch master nothing to commit (working directory clean) – Disco Sep 14 '11 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`. – Platinum Azure Sep 14 '11 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 '11 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 03 '20 at 20:11
  • Worth noting that `.gitkeep` is a convention; any filename works, but it has to be a file, and not a folder – Zoe Jul 07 '22 at 20:55
8

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

git branch [branchName]

and then

git checkout [branchName]
Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40
blacksta500
  • 79
  • 2
  • 3
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]

Dapo Momodu
  • 184
  • 1
  • 8
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.

Sachin Sridhar
  • 434
  • 4
  • 13
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.

Dan Ray
  • 21,623
  • 6
  • 63
  • 87
  • 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 '11 at 13:45
  • also in the master branch the directory 'example' is not there (as i see in github) – Disco Sep 14 '11 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 '11 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 '11 at 16:08