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|
- Your branch may not be called master. This git started a new repository on
master, but GitLab's documentation says it usesmainwhen no other default is set, and many teams setinit.defaultBranchto main. Pushing a name you don't have fails with "src refspec master does not match any". Check withgit branch --show-currentand push that name. -uremembers where to push. Aftergit push -u origin main, the branch tracksorigin/main, so later pushes and pulls are plaingit pushandgit pull.- "rejected (fetch first)" means the remote moved. The teammate had pushed a README.
git pull --rebasefetched it and replayed my commit on top, so the history stayed a straight line, and the next push worked. -foverwrites 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.--force-with-leasechecks 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_Storeor thexcuserdatafolder, which holds per-user Xcode state. GitHub's Swift template ignoresxcuserdata/too and has more entries you may want. Make the file before the firstgit add; a file already committed stays tracked until you remove it from the index. - Read
git statusbefore committing.git add .takes everything in the folder. Look for API keys, certificates and.envfiles you didn't mean to share. -b mainnames 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://orgit@, not a plainhttp://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
- Run
git ls-files | grep -E "xcuserdata|DerivedData|\.DS_Store". Anything listed was committed before your.gitignore; remove it from the index withgit rm -r --cached <path>. - Run
git remote -vand check that no address starts withhttp://. - Search your shell history and team notes for
push -fand 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.
Keep reading on JR Devhub
Comments