or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Introduction to Git –- Fall 2023
Lecture 7: Teamwork
Slides: https://hackmd.io/@git-fall-2023/L7-teamwork#/
Teamwork
Git is very useful for teamwork.
You will often have three types of branches for a project/specific release:
NOTE: GitHub previously used "master", but is now using "main" as the name for the first/default branch in a repo
Teamwork
Concepts and commands
In these exercises we will use only a few commands. These have all been mentioned before in this course, but as a refresher I will briefly discuss a couple commands here, namely:
git fetch
: This is a primary command used to download contents from a remote repository.git push
: This is essentially the same as runninggit merge main
from inside the remote repository. It is mostly used to upload local changes to a remote repository.git pull
: This will fetch the latest changes from the current branch from a remote, then apply the changes to your local copy of the branch. It is similar to doing a fetch and a merge.Git push
or in some cases just
where the default behaviour is pushing to repository "origin" and the same branch as the local.
Before pushing:
After pushing:
Pushing a staged and committed file:
Git pull
Fetch the given remote's copy of the current branch and merge to the local copy:
or often just
Hint
If you have forgotten to pull before staging and committing new stuff, and your colleague has added something to the remote repository in between, this is a handy command:
It fetches the remote content but does not create a new merge commit.
Assume this situation:
Now we do a
git pull
:Pull from remote repo, new file
Let us do an example where there is a new file on the remote repository:
Example: creating a new branch, pushing changes
Here we create a new branch on a remote repository and add a file to it, then push:
Create the branch
Switch to it
I then create a file on my new branch. After this, check the status and log
Example continued
Note: when you do this it should be coloured for easier readability
Example continued
I stage and commit the file.
I will check to confirm I am on the right branch:
Example continued
Let us again check with
git status
andgit log ...
Example continued
Push your changes with
git push origin -u yourbranchname
(or withgit push -u origin HEAD
)Let us check the status
Example: Merging branches
We will merge the branches from the command line. Let us first see which branches exist in my repo - in this case I will check for both local and remote branches
I have a bunch of branches (created previously by me and another user, for testing). As you can see, I am on my newest branch "mynewbranch"
Example - continued
Switch to the branch we are merging to (main)
Do the merge (it will also open an editor where you should write a commit message for why the merge should happen)
Example - continued
Let us do a status check
Pushing
Checking status again
First exercise - GitHub (if not setup already)
Second exercise, SSH keys (if you have not set up already)
In this exercise you create SSH keys and upload to GitHub. Then test that it works.
Create a new SSH key
Open a terminal (Git Bash on Windows). In the command below, "GitHub" is a label added to the key for clarity. You can add any you want:
a. Do this
b. If you have an older system, this may work better
You will be asked for a file to save the key. Unless you have an existing SSH key, accept the default.
Enter a passphrase and repeat it.
Second exercise (cont.), SSH keys (if you have not set up already)
.ssh
folder, open the fileid_rsa.pub
and copy it. Do NOT add any newlines or whitespace!Adding the SSH key to GitHub
Testing the SSH keys
$ ssh -T git@github.com
Third exercise, clone, push, pull
We now have SSH keys set up. Time to test it from your own machine:
git pull
and see that it works. You will have to enter the key passphrase.git add
,git commit
)ssh-add
to add the key. Then you will only be asked for the passphrase once per session. This is relatively safe on Linux and macOS, but not on Windows where it usually saves the key passphrase permanently.Fourth exercise, teamwork, clone, push, pull
One of you should create a repository on GitHub and invite their team. Remember, on the GitHub webpage the option to create a new repository is in the top right corner - click the "+". To add members: "Settings" -> "Manage access".
git status
. Do agit log --graph --oneline --decorate --all
git pull
before you stage and commit your file and also the team members should use different names for their files. See the changes appear when you do agit pull
after all have added their file(s).git pull --rebase
before you re-dogit push
git status
andgit log --graph --oneline --decorate --all
before and after each step. Push the files to the repository. Check the log and status again.Fifth exercise, teamwork, branches and merging
git branch yourbranchname
where you put any name you want for the new branch.git checkout yourbranchname
git log
andgit status
to see any changes.git log
andgit status
git push origin -u yourbranchname
(or withgit push -u origin HEAD
for a fast way when using the same name)git pull
git status
,git branch
, andgit log
to see what has happened.Sixth exercise, teamwork, branches and merging, pull requests
git pull
(on the command line)git status
,git branch --all
, andgit log --graph --oneline --decorate --all
to see what has happened.Note: It is possible to make the main branch "protected" so it is not changed without a review from the owner. Try doing this (on GitHub).
Seventh exercise, teamwork and branches
git branch -r
git branch
git status
to see which branch you are on.git branch -a
to see all local and remote branchesgit pull
from the command line to get a list of all branches. Switch to the branch you created on GitHub withgit checkout --track origin/mynewbranch
. Again dogit branch
to see which branch you are on.git status
,git log
). Push the changes.Seventh exercise, teamwork and branches - continued
git pull
(on the command line)git status
,git branch
, andgit log
to see what has happened. If you want a "prettier" and sometimes easier to read view, usegit log --graph --oneline --decorate --all
Eighth exercise, deleting branches
git branch -D <alsomyownbranch
>git status
,git log
andgit branch
to see what has happenedgit pull
andgit fetch
, possibly with suitable flags will get it back for you)Ninth exercise, merge conflicts
In this exercise everyone in the team will be working in the same branch, for instance the main branch.
Merge conflicts generally happen when two (or more) teammembers edit the same file and the same line, or when one edits a file and another deletes it.
git pull --rebase
before pushing.git pull
git status
,git branch
, andgit log
to see what has happened. Try to resolve the conflict.git rm file
). What happens when you push your work? You should get a conflict. Try and resolve the conflict. Should the file be kept or deleted?