Connect a Local Folder to a GitHub Repository and Ignore Specific Folders
1 Introduction
This blog post introduce how to connect local files to GitHub and
selectively sync specific folders, like your coding work, while
excluding others (e.g., data folders). You can achieve this by using a
.gitignore file to tell Git what to exclude when pushing
changes to your GitHub repository.
2 Connect a Local Folder to a Github Repository
Create a repository on Github

- GitHub provides instructions to help you set up your repository.

Open terminal in the working folder and run commands as instructed
1
2
3
4
5
6
7echo "# Repository" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/tehya-tan/Repository.git
git push -u origin main
- Now, this folder is connected to the Git repository on the account.
3 Ignore Folder for Git Repositories
Create or open the
.gitignorefile: If you don't already have a.gitignorefile in your project's root directory, create one. You can use any text editor or run the following command to create this file.1
touch .gitignore
Add folder names to
.gitignoreand save: Open the.gitignorefile and add the names of the folders you want to ignore, each on a separate line.For example, if you want to ignore two folders named
folder1andfolder2, add the following lines to.gitignore:1
2folder1/
folder2/You can continue adding more folder names, each on a new line.
Commit the
.gitignorefile:1
2git add .gitignore
git commit -m "Added folders to .gitignore"Note: The
.gitignorefile itself needs to be committed to make the ignore rules effective.
4 Push all changes except the ignored folders
- Enter
git add --allto add all the files or changes to the repository. - Enter
git statusto see the changes to be committed. - Enter
git commit -m '<commit_message>'at the command line to commit new files/changes to the local repository. - Use
git pushto publish your local commits
We also can push changes only on specific folder and files by replacing the step 1 by
1 | git add folder |