GitLab - Add a File
GitLab - Adding a File to a Project
Section titled “GitLab - Adding a File to a Project”In GitLab, as with any Git-based project, you can add files to your project in several ways. The two primary methods are using the command-line interface (CLI) with Git commands or using the GitLab web interface.
Method 1: Adding a File using the Command Line Interface (CLI)
Section titled “Method 1: Adding a File using the Command Line Interface (CLI)”This method involves using Git commands in your local project repository and then pushing the changes to GitLab.
Step 1: Navigate to your project directory
Open your terminal or command prompt and navigate to the root directory of your local Git repository.
$ cd /path/to/your/projectStep 2: Create the new file
You can create a new file using any text editor or a command like touch (on Linux/macOS) or echo.
$ touch new_document.txt# This creates an empty file named new_document.txtOr, create a file with some initial content:
$ echo "Hello World" > new_file.mdStep 3: Add the file to Git’s staging area
Tell Git to track this new file:
$ git add new_document.txt new_file.md# Or, to add all new/modified files:# $ git add .Step 4: Commit the changes
Save the changes to your local repository history with a descriptive commit message:
$ git commit -m "docs: Add new_document.txt and new_file.md"Step 5: Push the changes to GitLab
Upload your local commits to the remote repository on GitLab. Replace main with your branch name if different (e.g., master, feature-branch).
$ git push origin mainYour new file(s) will now be visible in your GitLab project.
Method 2: Adding a File using the GitLab Web Interface
Section titled “Method 2: Adding a File using the GitLab Web Interface”GitLab’s web interface allows you to create and edit files directly in your browser.
Step 1: Navigate to your project in GitLab
Open your web browser and go to your project’s page on GitLab.
Step 2: Add a new file
Typically, above the file list in your repository view, there is a ’+’ button or a button labeled ‘Add file’ or similar (e.g., a dropdown next to the branch name). Click this and select an option like ‘New file’.
If you are deep within a directory structure, the option to add a file might be specific to that directory.
Step 3: Name the file and add content
You’ll be taken to a new page with an editor.
- In the ‘File name’ field, type the name of your new file (e.g.,
README.md,src/app.js). You can also create directories by including them in the path (e.g.,docs/guide.mdwill create adocsdirectory if it doesn’t exist). - In the large text area below, add the content for your file.
Step 4: Commit the changes
Below the editor, you’ll find fields for your commit message.
- Write a concise and descriptive ‘Commit message’.
- Choose the ‘Target branch’ where you want to add this file. You can commit directly to an existing branch or choose to create a new branch for these changes (recommended for collaborative workflows to start a new merge request).
Click the ‘Commit changes’ button.
Step 5: Confirmation
After committing, you should be redirected to the repository view or the file itself, and your new file will be listed. If you opted to create a new branch, GitLab will usually prompt you to create a merge request.
Which Method to Choose?
Section titled “Which Method to Choose?”CLI: Preferred by developers for complex changes, working offline, or when already working in the terminal. Offers full Git power.
Web Interface: Convenient for quick additions, small edits (like fixing typos in documentation), or when you don’t have your local development environment set up. Excellent for users less familiar with Git commands.