Similar to my post on how to connect Git to Github, this post is how to use Git to connect to an enterprise internal Gitlab instance. First, find out what branches exist in your company and what their workflow is. It is typically like:
development -> testing -> production
So you will ‘checkout’ development, make changes locally by ‘commiting’, then ‘pull/push’ often to development branch. When you reach a checkpoint, you will ‘merge’ development into testing/production assuming the code passes all checks.
Initial Setup – Install ‘Git for Windows’ and ‘Github Deskop’.
Open ‘Git Bash for Windows’ and enter:
ssh-keygen -t rsa -C "yourEmail@company.com" -b 4096 git config --global user.email "yourEmail@company.com" git config --global user.name "Firstname Lastname"
In Gitlab, add the ssh key to your account. Just copy the .pub file contents to your clipboard and paste it in.
In Gitlab, create a personal access token. Give it ‘sudo’ and ‘api’ for this to work. Copy it to your clipboard.
In Github Desktop, click ‘Clone repository’.
In the URL field, type https://yourGitLabInstance/name-of-repo.git
It will try to clone the repo and fail because of authentication. This is to be expected. In the credentials popup, enter your email address and your personal access token from step 4.
Check out the development branch
cd /to/your/git/directory #and then git checkout development
In Github Desktop, just change the dropdown under ‘current branch’ to development.
Make a change to a file. For example, I just added a comment to the bottom of a file
Now, Stage all files since your last commit (which was never)
cd /to/your/git/directory # make sure you are on the correct branch - 'development'. NOTE: Run this command often as it will always tell you useful information. git status #and then git add --all # Commit them locally git commit -m "first change"
In Github Desktop: The changed files show up automatically, just add a message and commit.
- Adding your changes to Gitlab:
# Push them back to ‘company-repo’ under your branch
# The way it works is you first pull down changes, test to make sure your code works, and then push back up with your changes attached. Do this often!
git fetch origin git push origin development # In the future, you can just type 'git push' to see changes
In Github Desktop:
Just press the ‘fetch origin’ and ‘push origin’ button every so often.
- Merge with main branch – Production: NOTE: We will probably simplify this in the future, but for now follow this workflow
git checkout testing git pull testing git merge development git checkout production git pull production git merge testing
- Switch back to your local working branch and keep making changes for the next ‘Push to production’ (Steps 6- 10) repeatedly.
git checkout development