- A brief history of Git.
- PWSLab walkthrough.
- Configure your environment.
- Distributed version control.
- It does not rely on a connection to a central server.
- Many copies of the complete history.
- Powerful branching and merging.
- Adapts to nearly any workflow.
- Fast, reliable and stable file format.
Use the tools at your disposal when you get stuck.
- Use ‘git help <command>’ command.
- Use Google.
- Read the documentation at https://git-scm.com/.
Configure your Environment
- Windows: Install ‘Git for Windows’ - https://git-for-windows.github.io/
- Mac: Type ‘git’ in the Terminal application. If it’s not installed, it will prompt you to install it.
- Debian (Ubuntu): ‘sudo apt-get install git-all’ or Red Hat ‘sudo yum install git-all’
- Configure Git.
- Configure SSH Key.
- Create a project.
- Feature branching.
- Merge requests.
- Feedback and Collaboration.
One-time configuration of the Git client:
git config --global user.name "Your Name"
git config --global user.email firstname.lastname@example.org
ssh-keygen -t rsa -b 4096 -C "you@computer-name"
# You will be prompted for the following information. Press enter to accept the defaults. Defaults appear in parentheses.
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/you/.ssh/id_rsa.
Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
The key fingerprint is:
Copy your public key and add it to your PWSLab profile:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQEL17Ufacg8cDhlQMS5NhV8z3GHZdhCrZbl4gz email@example.com
Create a Project
- Create a project in your user namespace.
- Choose to import from ‘Any Repo by URL’ and use https://pwslab.org/pwslab/training-examples.git
- Create a ‘development’ or ‘workspace’ directory in your home directory.
- Clone the ‘training-examples’ project.
git clone firstname.lastname@example.org:/training-examples.git
New files that Git has not been told to track previously.
Files that have been modified but are not committed.
Modified files that have been marked to go in the next commit.
- Edit ‘edit_this_file.rb’ in ‘training-examples’.
- See it listed as a changed file (working area).
- View the differences.
- Stage the file.
- Push the commit to the remote.
- View the git log.
# Edit `edit_this_file.rb`
git commit -m 'My change'
git push origin master
- Efficient parallel workflow for teams.
- Develop each feature in a branch.
- Keeps changes isolated.
- Consider a 1-to-1 link to issues.
- Push branches to the server frequently.
- Hint: This is a cheap backup for your work-in-progress code.
Feature branching steps
- Create a new feature branch called ‘squash_some_bugs’.
- Edit ‘bugs.rb’ and remove all the bugs.
Commands (feature branching)
git checkout -b squash_some_bugs
# Edit `bugs.rb`
git add bugs.rb
git commit -m 'Fix some buggy code'
git push origin squash_some_bugs
- When you want feedback to create a merge request.
- Target is the ‘default’ branch (usually master).
- Assign or mention the person you would like to review.
- Add ‘WIP’ to the title if it’s a work in progress.
- When accepting, always delete the branch.
- Anyone can comment, not just the assignee.
- Push corrections to the same branch.
Merge requests steps
Create your first merge request:
- Use the blue button in the activity feed.
- View the diff (changes) and leave a comment.
- Push a new commit to the same branch.
- Review the changes again and notice the update.
Feedback and Collaboration
- Merge requests are a time for feedback and collaboration.
- Giving feedback is hard.
- Be as kind as possible.
- Receiving feedback is hard.
- Be as receptive as possible.
- Feedback is about the best code, not the person. You are not your code.
Feedback and Collaboration resources
- Useful for marking deployments and releases.
- Annotated tags are an unchangeable part of Git history.
- Soft/lightweight tags can be set and removed at will.
- Many projects combine an annotated release tag with a stable branch.
- Consider setting deployment/release tags automatically.
- Create a lightweight tag.
- Create an annotated tag.
- Push the tags to the remote repository.
git checkout master
# Lightweight tag
git tag my_lightweight_tag
# Annotated tag
git tag -a v1.0 -m ‘Version 1.0’
git push origin --tags
- Happen often.
- Learning to fix conflicts is hard.
- Practice makes perfect.
- Force push after fixing conflicts. Be careful!
Merge conflicts steps
- Checkout a new branch and edit conflicts.rb. Add ‘Line4’ and ‘Line5’.
- Commit and push.
- Checkout master and edit conflicts.rb. Add ‘Line6’ and ‘Line7’ below ‘Line3’.
- Commit and push to master.
- Create a merge request.
Merge conflicts commands
After creating a merge request you should notice that conflicts exist. Resolve the conflicts locally by rebasing.
git rebase master
# Fix conflicts by editing the files.
git add conflicts.rb
git commit -m 'Fix conflicts'
git rebase --continue
git push origin -f
Rebase with squash
You may end up with a commit log that looks like this:
Fix issue #13
Does this work?
Squash these into meaningful commits using an interactive rebase.
Rebase with squash commands
Squash the commits on the same branch we used for the merge conflicts step.
git rebase -i master
In the editor, leave the first commit as ‘pick’ and set others to ‘fixup’.
Also, let us know if the article is helpful!
List of PWSLab CI/CD Environment Variables
Introduction This document enlists a set of pre-defined environment variables accepted by PWSLab CI/CD for the local environment of the Runner. These can be referenced directly in the .pwslab.yml file or via the Project's Settings > CI/CD > ...
How To Change Git Remote URL?
Introduction Git remote is a pointer that refers to another copy of the repository that is usually hosted on a remote server. In some situations, like when the remote repository is migrated to another host, you need to change the remote’s URL. This ...
How to duplicate a Git repository? Repository mirroring simplified.
To duplicate a repository without forking it, you can run a special clone command, then mirror-push to the new repository. Before you can duplicate a repository and push to your new copy, or mirror, of the repository, you must create the new ...
Simple steps to synchronise a Remote Fork in a Git repository
Introduction When you collaborate in any upstream project (like an open-source project) it is likely to be asked to make a fork of the main repository. A fork is a copy of the project in your Git account. This duplicate allows you to freely ...
How to apply Git Patches?
Patch is a text file, whose contents are similar to Git diff, but along with code, it also has metadata about commits; e.g., commit ID, date, commit message, etc. We can create a patch from commits and other people can apply them to their repository. ...