Introduction to Git –- Fall 2020
Learn More →
Learn More →
Learn More →
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 running git merge master
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 <remote-repo> <branch>
or in some cases just
$ git push
where the default behaviour is pushing to repository "origin" and the same branch as the local.
Before pushing:
digraph {
rankdir=LR
node [shape=circle width=0.5 fixedsize=shape]
edge [arrowhead=none][shape=none]
"a" -> "b"
a [fixedsize=true label=" "]
"b" -> "c"
b [fixedsize=true label=" "]
"c" -> "d"
c [fixedsize=true label=" "]
d [fixedsize=true label=" " style=filled fillcolor=blue]
"Master" -> "d" [style=dashed arrowhead=normal]
"Origin/Master" -> "b" [style=dashed arrowhead=normal]
Master [shape=box width=0.8 style=filled fillcolor=lightblue]
"Origin/Master" [shape=box width=1.2 style=filled fillcolor=lightblue]
}
After pushing:
digraph {
rankdir=LR
node [shape=circle width=0.5 fixedsize=shape]
edge [arrowhead=none][shape=none]
"a" -> "b"
a [fixedsize=true label=" "]
"b" -> "c"
b [fixedsize=true label=" "]
"c" -> "d"
c [fixedsize=true label=" "]
d [fixedsize=true label=" " style=filled fillcolor=blue]
"Master" -> "d" [style=dashed arrowhead=normal]
"Origin/Master" -> "d" [style=dashed arrowhead=normal]
Master [shape=box width=0.8 style=filled fillcolor=lightblue]
"Origin/Master" [shape=box width=1.2 style=filled fillcolor=lightblue]
}
Pushing a staged and committed file:
$ git push origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 283 bytes | 283.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:bbrydsoe/testrepo.git
f91d59b..55f35b9 master -> master
~/teamwork$ git log
commit 55f35b94d5e606af94cbc622f4517109ddb69f21 (HEAD -> master, origin/master)
Author: Birgitte Brydsö <bbrydsoe@cs.umu.se>
Date: Tue Sep 29 16:40:58 2020 +0200
Adding a file
Fetch the given remote's copy of the current branch and merge to the local copy:
$ git pull <remote-repo>
or often just
$ git pull
If you have forgotten to pull before staging and committing new stuff, and your colleague has added something to the remote repository this is handy:
$ git pull --rebase <remote>
It fetches the remote content but does not create a new merge commit.
Assume this situation:
digraph {
rankdir=LR
node [shape=circle width=0.5 fixedsize=shape]
edge [arrowhead=none][shape=none]
"d" -> "e"
"e" -> "a"
"e" -> "f"
a [fixedsize=true label="a"]
"a" -> "b"
"b" -> "c"
b [fixedsize=true label="b"]
c [fixedsize=true label="c"]
d [fixedsize=true label="d"]
"f" -> "g"
"Master" -> "c" [style=dashed arrowhead=normal]
"Origin/Master" -> "e" [style=dashed arrowhead=normal]
Master [shape=box width=0.8 style=filled fillcolor=lightblue]
"Origin/Master" [shape=box width=1.2 style=filled fillcolor=lightblue]
}
Now we do a git pull
:
digraph {
rankdir=LR
node [shape=circle width=0.5 fixedsize=shape]
edge [arrowhead=none][shape=none]
"d" -> "e"
"e" -> "a"
"e" -> "f"
a [fixedsize=true label="a"]
"a" -> "b"
"b" -> "c"
b [fixedsize=true label="b"]
c [fixedsize=true label="c"]
d [fixedsize=true label="d"]
"f" -> "g"
"c" -> "h"
"g" -> "h"
"Master" -> "c" [style=dashed arrowhead=normal]
"Origin/Master" -> "c" [style=dashed arrowhead=normal]
Master [shape=box width=0.8 style=filled fillcolor=lightblue]
"Origin/Master" [shape=box width=1.2 style=filled fillcolor=lightblue]
}
Let us do an example where there is a new file on the remote repository:
$ git pull
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
From github.com:bbrydsoe/testrepo
55f35b9..e6ca68c master -> origin/master
Updating 55f35b9..e6ca68c
Fast-forward
newfile.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 newfile.txt
git clone
. Use the HTTPS address (click CODE to find it). See example:git add
, git commit
, git push
)git pull
before you stage and commit your file and use different names for your files. See the changes appear after git pull
git pull --rebase
before you do git push
It is easier to use SSH keys than to enter username and password every time. In this exercise you create SSH keys and upload to GitHub. Then test that it works.
Create a new SSH key
$ ssh-keygen -t rsa -b 4096 -C "GitHub"
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_rsa
.ssh
folder, open the file id_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
$ ssh -T git@github.com
The authenticity of host 'github.com (140.82.121.4)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,140.82.121.4' (RSA) to the list of known hosts.
Enter passphrase for key '/home/bbrydsoe/.ssh/id_rsa':
Hi bbrydsoe! You've successfully authenticated, but GitHub does not provide shell access.
We now have SSH keys set up. Try and repeat some of the things that was done in the first exercise:
git pull
and see that you no longer have to enter your GitHub username and password, but you do have to enter the key passphrase.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.git branch yourbranchname
where you put any name you want for the new branch.git push origin -u yourbranchname
git pull
git status
, git branch
, and git log
to see what has happened.git pull
git status
, git branch
, and git log
to see what has happened.It is possible to make the master branch "protected" so it is not changed without a review from the owner.