Install Git. How to: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
Obtain access to GitLab. We use GitLab CI to build our Python packages and Docker Images and to deploy them to our development and production environments. See: https://about.gitlab.com/
git clone
relevant repositories. git clone
creates a copy of the specified repository which you intend to make changes to. For example, on your command line, make sure you are in the appropriate directory. For this example, I created a new directory called “testing” using the command mkdir testing
Go to your GitLab project and click on the “Clone” button, which should be on the upper right hand. From the dropdown, click on the "Copy URL" icon under "Clone with HTTPS”
From your command line, run the command git clone <url>
, where the <url>
is the URL you just copied. That’s it!
For more information, visit this site.
git branch
in the appropriate repository to view a list of existing branches, as well as the current branch you are on.Note: Each team is different! Make sure you check in with your team about naming conventions, commit message conventions, etc.
git branch -m new-renamed-branch
Let’s say you want to check out a new branch, but you have uncommitted changes on the current branch you are on that you want to completely discard. If you want to ensure that those changes don’t get carried over to the new branch, run the following commands:
git reset --hard
- This resets all of your local changes to the last commit.
git checkout master
- This allows you to “check out” or navigate to another branch. In this case, it’s the master branch.
git pull --rebase
- This essentially fetches upstream changes and rebases your branch.
git checkout -b new_branch
- The -b
in this command creates a new branch off of the branch that you are on.
[caption id="attachment_4390" align="aligncenter" width="770"] From https://education.github.com/git-cheat-sheet-education.pdf[/caption]
git add -u
- This command only adds changed files that are already tracked by git. It is usually recommended to add files individually, so you’re not faced with accidentally committing secrets or random files that you didn’t intend to.
git commit -m "your commit message"
git push origin your-branch-name
Keep every merge request specific to one change. Before pushing your branch and submitting an MR, run git diff master
to ensure that your branch only has the specific feature/change you intended
The GitLab CI/CD (Continuous Integration/Continuous Development) is a system that can be used for code deployment and development. GitLab uses pipelines to run jobs (instructions for a runner to execute and compile code) that get executed in stages. After pushing changes and creating a new merge request, make sure the pipeline succeeds before assigning a code reviewer.
Rebasing integrates changes from one branch into another. You typically do a rebase when the master branch is ahead of your branch, and you want to integrate those changes into your branch. Rebasing differs from merging by rewriting the commit history in order to produce a straight, linear succession of commits. Often, if you see a message in GitLab that says “There are merge conflicts”, it is likely that you need to rebase your branch.
git checkout master
git pull
Switch to branch (i.e. git checkout your-branch-name)
git rebase -r origin/master
git status
Resolve changes in files that are highlighted.
git add -u
git rebase --continue
git push --force-with-lease origin your-branch-name
. Don’t be alarmed, this is normal! This is important because otherwise it could overwrite someone else’s work.git reset HEAD~1
. Additionally, if you’d like to undo a few commits, you may change the “1” in the above command to specify the number of commits you’d like to reset back to.git restore
to undo everything. If you’ve staged something and want to unstage it, run git restore --staged foo
, where foo
is a file name.git reset --hard
resets the whole branch to the last commit -- be careful doing this! It will undo all of your changes.Squashing commits results in a clean Git history which improves readability for your team. For example, say you have a commit history like so:
428239482hfs2... Added new UI feature, JIRA #321 1381ndj1o193... fixed error in failing test 32891njk189... small typo fix for other failing test 289b3kj723... made suggested changes 327823jfds89… hopefully this is the last! 382nvs328h... implemented small feature 923n9ds9824... last code iteration
Well, that simply gives me a headache to look at. Instead, after squashing your commits, the commit history will look something like this:428239482hfs2... Added new UI feature, JIRA #321
When you are ready to merge, GitLab provides a “merge” button on the merge request. It is best practice to use this button!Switch to master branch locally by running git checkout master
Run git pull
Delete branch locally using <code>git branch -d branch-name</code>
Always checkout a new branch from the master branch. To do this, follow the steps below in your terminal in the appropriate repository:
git checkout master
git pull
git checkout -b your-branch-name
Forget a Git command? Run git help
to view a list of the 21 most common Git commands.
Git tips: https://about.gitlab.com/blog/2020/04/07/15-git-tips-improve-workflow/
Git aliases: https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases - Git aliases are useful because they allow you to do less typing -- efficiency is key in development! i.e. using “git co” instead of “git checkout”.
(Feature photo by Xavi Cabrera on Unsplash)
Posted by Taylor Penberthy