Short answer

To put an Xcode project on GitLab from Terminal: add a .gitignore, then git init, git add ., git commit, git remote add origin <project URL> and git push -u origin main, using the branch name your repository really has. If the push is rejected because someone pushed first, run git pull --rebase and push again. Avoid git push -f: in a test it deleted a teammate's commit from the remote. --force-with-lease refused in the same situation.

SWIFT TIPS · XCODE · GIT

First posted on May 24, 2018, as "Update push to gitlab all file in Terminal". Rewritten on November 18, 2026. Every command was run on a Mac with git 2.50.1 on macOS 26.0.1, in a throwaway folder where local bare repositories stood in for the GitLab project, with no network, no personal git settings and a sample author name. No real GitLab server was used.

In 2018 I wrote down the commands for putting a project on a GitLab server from Terminal: go to the project folder, git init, git remote add origin with the project's address, git add ., git commit -m "Initial commit", and git push -u origin master. When a push was rejected, I found an answer on Stack Overflow and pasted most of it into the post: pull with rebase, then push.

Those commands still work. This rewrite runs each of them, including the ones that fail, explains them in my own words instead of a pasted answer, and adds the two things the post didn't have: a .gitignore for Xcode, and why the post's last suggestion, git push -u -f, is dangerous.

Running the commands

The test made an empty bare repository to play the GitLab project, a small app folder with an Xcode-style layout, and a second clone to play a teammate:

## 1. Which branch does a new repository start on?
git init (no config): HEAD -> master
## 2. First push of an existing project
files staged: .gitignore MyApp.xcodeproj/project.pbxproj MyApp/App.swift 

$ git push -u origin master
  error: src refspec master does not match any
  error: failed to push some refs to '../gitlab-project.git'

$ git push -u origin main
  To ../gitlab-project.git
   * [new branch]      main -> main
  branch 'main' set up to track 'origin/main'.
  tracking: origin/main
## 3. Someone else pushed first

$ git push
  To ../gitlab-project.git
   ! [rejected]        main -> main (fetch first)
  error: failed to push some refs to '../gitlab-project.git'

$ git pull --rebase
  From ../gitlab-project
     54e8f1a..82b02a7  main       -> origin/main
  Rebasing (1/1)
Successfully rebased and updated refs/heads/main.

$ git push
  To ../gitlab-project.git
     82b02a7..9aed666  main -> main
  remote history: Change greeting|Add README|Initial commit|
## 4. Force push over a teammate's work
  remote before: Teammate: add notes|Change greeting|Add README|Initial commit|

$ git push --force-with-lease
  To ../gitlab-project.git
   ! [rejected]        main -> main (stale info)
  error: failed to push some refs to '../gitlab-project.git'

$ git push -u -f origin main
  To ../gitlab-project.git
   + dcdd0a0...0d5bb78 main -> main (forced update)
  branch 'main' set up to track 'origin/main'.
  remote after:  Me: another change|Change greeting|Add README|Initial commit|
  1. Your branch may not be called master. This git started a new repository on master, but GitLab's documentation says it uses main when no other default is set, and many teams set init.defaultBranch to main. Pushing a name you don't have fails with "src refspec master does not match any". Check with git branch --show-current and push that name.
  2. -u remembers where to push. After git push -u origin main, the branch tracks origin/main, so later pushes and pulls are plain git push and git pull.
  3. "rejected (fetch first)" means the remote moved. The teammate had pushed a README. git pull --rebase fetched it and replayed my commit on top, so the history stayed a straight line, and the next push worked.
  4. -f overwrites the remote. The teammate then pushed "add notes". My force push replaced the remote branch with my local one, and their commit disappeared from the remote history. Git's own documentation says forcing can cause the remote repository to lose commits.
  5. --force-with-lease checks first. It only overwrites the remote branch if it still matches what your last fetch saw. Here it didn't, so git answered "stale info" and pushed nothing.

A first push, step by step

# In the folder that holds MyApp.xcodeproj
cd ~/Projects/MyApp

# 1. Ignore what Xcode generates for you
cat > .gitignore <<'EOF'
.DS_Store
xcuserdata/
DerivedData/
build/
EOF

# 2. Make the first commit
git init -b main
git add .
git status            # read the list before you commit
git commit -m "Initial commit"

# 3. Connect the empty GitLab project and push
git remote add origin https://gitlab.example.com/team/my-app.git
git push -u origin main
  • The .gitignore comes first. In the test, git add . staged the project file and the Swift file but not .DS_Store or the xcuserdata folder, which holds per-user Xcode state. GitHub's Swift template ignores xcuserdata/ too and has more entries you may want. Make the file before the first git add; a file already committed stays tracked until you remove it from the index.
  • Read git status before committing. git add . takes everything in the folder. Look for API keys, certificates and .env files you didn't mean to share.
  • -b main names the first branch, so it matches the project's default branch. Use the name your GitLab project shows.
  • HTTPS or SSH. GitLab shows both addresses on the project page. Use https:// or git@, not a plain http:// address, so your code and credentials don't travel unencrypted.

When a push is rejected

git pull --rebase     # get their commits, put yours on top
# fix any conflicts, then: git add <file> and git rebase --continue
git push

If you really need to rewrite the remote branch, for example after amending a commit nobody else has pulled, use git push --force-with-lease instead of -f. It refuses when someone pushed something you haven't seen.

A quick check for your project

  1. Run git ls-files | grep -E "xcuserdata|DerivedData|\.DS_Store". Anything listed was committed before your .gitignore; remove it from the index with git rm -r --cached <path>.
  2. Run git remote -v and check that no address starts with http://.
  3. Search your shell history and team notes for push -f and replace it with --force-with-lease.

What the 2018 version said

Besides the first-push commands, the post listed git log (press q to leave it) and git status, and then a long answer copied from Stack Overflow about git pull --rebase, tracking branches and git reset --mixed origin/master. I've summarized the part that matters above; the original answers are linked in the sources. The post ended with a "useful, recommended" sequence that finished with git push -u -f origin master, which is the force push from item 4. It also showed a screenshot of a successful push, which I left out because it showed a private server's address.

Sources