...
In the previous sections you have worked only with a local repository. The next step is to share this content with a remote repository, which we manage with Stash. You will first have to configure your Stash account:
- Login to our Stash server with your Rtsys account information. If you haven't received your password yet, please wait until you have that password before you continue this tutorial.
- Through the button in the top right corner, access your profile.
- Switch to the SSH keys tab.
- Click Add Key and upload a public SSH key that you want to use to access the repository.
- If you don't have an SSH key: use the shell command
ssh-keygen
, confirm the default destination file~/.ssh/id_rsa
, and choose whether to give a passphrase. If you have a passphrase, you need to enter it whenever you use your SSH key for the first time in a session. You can omit the passphrase, but that makes the key less secure. As result, the tool generates a private key~/.ssh/id_rsa
, which has to be kept secret, and a public key~/.ssh/id_rsa.pub
.
- If you don't have an SSH key: use the shell command
Usually it is sufficient to have only one local copy of a Git repository. However, in this tutorial you will create a second copy in order to "simulate" what can happen if two users access the same remote repository: imagine the directories turing
and turing2
are each managed by a different user. You will simulate the resulting interference by switching your working directory between these two.
- Go to Stash → Create Project and call it "personal-<login>", replacing <login> with your own login name. Use your uppercase login name as project key, e.g. "MSP".
- Go to the Permissions tab of the project page and add the user "msp" as observer.
- On the project page, select Create Repository and name it "turing".
- Copy the SSH URL shown in the top right and email it to msp@informatik.uni-kiel.de. This will serve as proof for your work on this tutorial.
Transfer your
master
branch to the new server-side repository. Replace the URL in the following command by the one copied from Stash:No Format $ git remote add stash ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git $ git push stash master Counting objects: 15, done. Delta compression using up to 16 threads. Compressing objects: 100% (13/13), done. Writing objects: 100% (15/15), 1.54 KiB, done. Total 15 (delta 3), reused 0 (delta 0) To ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git * [new branch] master -> master
The first command adds a remote named "stash" to your local repository, which is just a bookmark for the long URL. The second command transfers the
master
branch to the server, which is called pushing. After that is done, reload the Stash page in your browser, and you see all changes that are transferred to the server-side repository.Create a local clone of your remote repository (replace the URL accordingly):
No Format $ cd .. $ git clone ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git turing2 Initialized empty Git repository in /home/msp/tmp/turing2/.git/ remote: Counting objects: 15, done. remote: Compressing objects: 100% (13/13), done. remote: Total 15 (delta 3), reused 0 (delta 0) Receiving objects: 100% (15/15), done. Resolving deltas: 100% (3/3), done. $ cd turing2
The
clone
command automatically creates a remote namedorigin
in the new local repository, which is set to the given URL. You will use this second clone to simulate another user with access to the repository.- Edit the file
examples.txt
in the new clone (turing2
): replace"a"
in line 6 by"c"
and correct the tape representations in lines 9, 14, and 19 accordingly. Commit the change. Push the new commit to the server:
No Format $ git push Counting objects: 5, done. Delta compression using up to 16 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 362 bytes, done. Total 3 (delta 1), reused 0 (delta 0) To ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git 8af2d50..1d1577f master -> master
In this case the push command can be used without arguments, which means that it pushes all branches as configured in
.git/config
:No Format $ more .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git [branch "master"] remote = origin merge = refs/heads/master
Here the branch
master
is linked with the remoteorigin
, hencegit push
does the same asgit push origin master
.Go back to the original local repository and check out the
master
branch:No Format $ cd ../turing $ git checkout master Switched to branch 'master'
Merge the
sketches
branch intomaster
:No Format $ git merge sketches Updating 8af2d50..21d5ddb Fast-forward examples.txt | 5 +++++ notes.txt | 1 + 2 files changed, 6 insertions(+), 0 deletions(-)
Now your local
master
branch and the one on the server-side repository have divergedFetch the server-side changes:
No Format $ git fetch stash remote: Counting objects: 5, done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git 8af2d50..1d1577f master -> stash/master
Now the change to
examples.txt
that was previously committed in theturing2
repository is stored in a remote tracking branch namedstash/master
:No Format $ git branch -a * master sketches remotes/stash/master
You can analyze the remote tracking branch using the
log
andshow
commands. However, you should never directly modify a remote tracking branch.You can merge the remote changes into your local
master
branch with the following command:No Format $ git merge stash/master Auto-merging examples.txt Merge made by recursive. examples.txt | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
Since this combination of
fetch
andmerge
is used very often, Git offers a shortcut for it, namely thepull
command. In this case the according command would have beengit pull stash master
.Push the merged branch to the server, and then push the
sketches
branch, which is not on the server yet:No Format $ git push stash master Counting objects: 23, done. Delta compression using up to 16 threads. Compressing objects: 100% (14/14), done. Writing objects: 100% (14/14), 1.65 KiB, done. Total 14 (delta 4), reused 0 (delta 0) To ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git 1d1577f..957f686 master -> master $ git push stash sketches Total 0 (delta 0), reused 0 (delta 0) To ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git * [new branch] sketches -> sketches
As next step change your working directory to the second local repository
turing2
, add the following line to the end ofnotes.txt
in theturing2
directory, and commit the change:No Format nopanel true TODO: formal definition
Trying to push this commit to the server results in the following error message:
No Format $ git push To ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details.
This is because you have modified the branch while working in the original
turing
repository, and these changes have to be merged with the new commit you have just made fornotes.txt
.The solution is to apply the
pull
command followed by thepush
command:No Format $ git pull remote: Counting objects: 23, done. remote: Compressing objects: 100% (14/14), done. remote: Total 14 (delta 4), reused 0 (delta 0) Unpacking objects: 100% (14/14), done. From ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git 1d1577f..957f686 master -> origin/master * [new branch] sketches -> origin/sketches Auto-merging notes.txt Merge made by recursive. examples.txt | 5 +++++ notes.txt | 1 + 2 files changed, 6 insertions(+), 0 deletions(-) $ git push Counting objects: 10, done. Delta compression using up to 16 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 673 bytes, done. Total 6 (delta 2), reused 0 (delta 0) To ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git 957f686..b58ded7 master -> master
While
pull
performs afetch
and amerge
,push
transfers the new merged branch to the server. Note that during the merge operation conflicts can occur. In that case you have to resolve them and commit the changes before you can push. When used without parameters like shown above,pull
lookes in.git/config
to determine which branches to pull from which remotes.In order to check out the
sketches
branch locally, which was previously pushed to the server, simply type the following command:No Format $ git checkout sketches Branch sketches set up to track remote branch sketches from origin. Switched to a new branch 'sketches'
This branch can be pushed and pulled with the server in the same way as you did for the
master
branch. Never check outorigin/sketches
, since that is a remote tracking branch!
...