Version Control, Part I - Git on a Single Machine Hans-Martin von Gaudecker Going back in time • How often in the research process does one believe a paper to be finished? • And when you think it is done, enter ... – ... supervisor, – ... co-authors, – ... conference participants, – ... referees Going back in time • Often long lags in between changes in beliefs • Project changes in the meantime – Suddenly you’re not able to reproduce the results you had at the time you submitted {paper, thesis} • Need to keep track of older versions Going back in time 1
31
Embed
Version Control, Part I - Git on a Single Machine€¦ · Version Control, Part I - Git on a Single Machine ... • Check Git’s status after including the .gitignore file in your
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Version Control, Part I - Git on a Single Machine
Hans-Martin von Gaudecker
Going back in time
• How often in the research process does one believe a paper to be finished?• And when you think it is done, enter ...
• Navigate to your project’s folder• Right-click on the project folder and select ”Git Bash”
Git on a Mac/Linux terminal
• Simply open a terminal in /Applications/Utilities and get started• Navigate to your project’s folder• Right-click on the project folder and select ”Git Bash”
6
Set up your git credentials
• Eventually, a large benefit of Git will be that it tracks who did what andwhen
• So first tell Git who you are and how you may be reached
On branch masternothing to commit, working directory clean
$ git branch
* masterbonn_ss_2013
Switching the branch: Git checkout again
• Use checkout to switch the branch
$ git checkout bonn_ss_2013
Switched to branch 'bonn_ss_2013'
$ git branch
master* bonn_ss_2013
17
DAG of the project, early 2013
Commits go to branch you are currently on
• Focus on one file: version_control.tex
[remove all SVN references from version_control.tex and add Sink book]}
$ git status
# On branch bonn_ss_2013# Changes not staged for commit:# (use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in working directory)## modified: version_control.tex#no changes added to commit (use "add" and/or "commit -a")
18
Commits go to branch you’re currently on
$ git add version_control.tex
$ git status
On branch bonn_ss_2013Changes to be committed:(use "reset HEAD <file>..." to unstage)
new file: version_control.tex
$ git commit -m "Sink book as the only VC reference."
[bonn_ss_2013 ad3fffa] Sink book as the only VC reference.1 file changed, 2 insertions(+), 4 deletions(-)
19
DAG of the project, early 2013
Editing files in another branch: Switch back andcontinue with the usual workflow
• Switch back to master branch, and add the Sink book to the list of SVNreferences in version\_control.tex
$ git checkout master
Switched to branch 'master'
[edit version_control.tex, add Sink book to references]
$ git add version_control.tex
$ git commit -m "Added Sink book to SVN references."
[master 4da7639] Added Sink book to SVN references.1 file changed, 1 insertion(+)
20
Renaming files
• Go back to new branch• Use Git to rename a file*Why?*
On branch masterChanges to be committed:(use "reset HEAD <file>..." to unstage)
Add the second file, edits
• Copy some content to new file
[create git_collaboration.tex]
[edit git_single_machine.tex and git_collaboration.tex]
$ git status# On branch bonn_ss_2013# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## renamed: version_control.tex -> git_single_machine.tex## Changes not staged for commit:# (use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in working directory)## modified: git_single_machine.tex## Untracked files:# (use "git add <file>..." to include in what will be committed)## git_collaboration.tex
$ git commit -m "Split up VC lecture in two parts."
[bonn_ss_2013 2fa686e] Split up VC lecture in two parts.}3 files changed, 10 insertions(+), 5 deletions(-)}create mode 100644 git_collaboration.tex}create mode 100644 git_single_machine.tex}delete mode 100644 version_control.tex}
Undo changes: Git checkout once more
[edit git_single_machine.tex]}
$ git status
On branch bonn_ss_2013Changes not staged for commit:(use "add <file>..." to update what will be committed)(use "checkout -- <file>..." to discard
changes in working directory)
modified: git_single_machine.tex
no changes added to commit (use "add" and/or "commit -a")
$ git checkout git_single_machine.tex
$ git status
On branch bonn_ss_2013nothing to commit, working directory clean
22
DAG of the project, early 2013
Merging
• Will continue to use Git: Merge it into master branch.
$ git checkout master
Switched to branch 'master'
$ git merge bonn_ss_2013
CONFLICT (modify/delete):version_control.tex deleted in bonn_ss_2013 and modified in HEAD.Version HEAD of version_control.tex left in tree.Automatic merge failed; fix conflicts and then commit the result.
• Message tells you exactly what you need to do!
23
Merging
$ git status
# On branch master# You have unmerged paths.# (fix conflicts and run "git commit")## Changes to be committed:## new file: git_collaboration.tex# new file: git_single_machine.tex## Unmerged paths:# (use "git add/rm <file>..." as appropriate to mark resolution)## deleted by them: version_control.tex#
$ git status# On branch master# All conflicts fixed but you are still merging.# (use "git commit" to conclude merge)## Changes to be committed:## new file: git_collaboration.tex# new file: git_single_machine.tex# deleted: version_control.tex#
$ git commit -m "Merge bonn_ss_2013 into master."
[master d7f6692] Merge bonn_ss_2013 into master.
24
Making it easy to find specific versions
• Maybe I will have to teach some students old technology again?• Will need to go back to the commit where I replaced Subversion by Git• Dig through hundreds of commits?• Tags to the rescue
DAG of the project, early 2013
Tagging in Action
• Check out a specific version using the hash (git log is your friend)
$ git checkout 4da7639Note: checking out '4da7639'.
You are in 'detached HEAD' state. You can look around,make experimental changes and commit them, and you candiscard any commits you make in this state withoutimpacting any branches by performing another checkout.
If you want to create a new branch to retain commitsyou create, you may do so (now or later) by using -bwith the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at 4da7639... Added Sink book to SVN references.
25
Tagging in Action
• Tag the commit, find it easily via git tag
$ git tag last_version_svn -m "Last version with SVN as the VC system."
$ git checkout master
Previous HEAD position was 4da7639...Added Sink book to SVN references.
Switched to branch 'master'
$ git tag
last_version_svn
Tagging in Action
Checking out a tag is just an alias for the hash...
$ git checkout last_version_svn
You are in 'detached HEAD' state. You can look around,make experimental changes and commit them, and you candiscard any commits you make in this state withoutimpacting any branches by performing another checkout.
If you want to create a new branch to retain commitsyou create, you may do so (now or later) by using -bwith the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at 4da7639... Added Sink book to SVN references.
$ git checkout master
26
Frontends vs. backends
• Frontend = the program you interact with• Backend = the program doing the work under the hood• The command line / Git Bash is only one of many frontends (user inter-
faces) for the underlying program Git• See example of TortoiseGit (integrates in Windows Explorer), Google will
show you a myriad of other options
Using another Git frontend: TortoiseGit
Navigate to your project’s folder
Using another Git frontend: TortoiseGit
Right-click on the folder and select ”Git-commit...”
27
Using another Git frontend: TortoiseGit
Type the commit message, and click ”OK”
When everything stops working ...
• Don’t panic!!!– Situation from the last commit is always in the repository– So be sure to commit frequently– Always solve problems immediately so that you won’t loose much
information should you have to go back• Won’t happen much now -- but things become a bit tricky once we use
Git for collaboration
28
Vocabulary (recap)
• DAG• Working copy• Repository• Index / staging area• Commit• Branch• Tag
• Some commands (e.g. git commit without a message) will trigger a de-fault editor if user input is required. This happens to be vim in mostcases
• It is helpful to remember two things– If you just want to leave: Press Esc, followed by :q (a colon followed
by q for quit)– If you want to make changes to the opened file: Press i for insert
mode and make your changes. Then press Esc followed by :wq (forwrite & quit)
Using Sublime as default text editor
• Enter the following commands to make Sublime Text your default editor
$ export EDITOR='subl -w '
$ export TEXEDIT='subl'
$ alias subl="subl --new-window"
• Those settings will only be active for the current session.
• If you want to make them persistent, you can copy those terms into your.bash_profile / .bashrc in your home directory.
.bashrc and Git bash
• When using Git bash on Windows you can also use a .bashrc file• Create a .bashrc file under ~/.bashrc, where ~ denotes your home direc-
tory• ~ is usually your C:\Users\<your user name> folder. Typing echo ~ in
the git bash terminal will tell you what that folder is
30
License for the course material
• [Links to the full legal text and the source text for this page.] You arefree:
– to Share to copy, distribute and transmit the work– to Remix to adapt the work
Under the following conditions:
• Attribution You must attribute the work in the manner speci-fied by the author or licensor (but not in any way that suggeststhat they endorse you or your use of the work).
With the understanding that:
• Waiver Any of the above condition scan be waived if you getpermission from the copyright holder.
• Public Domain Where the work or any of its elements in thepublic domain under applicable law, that status is in no wayaffected by the license.
• Other Rights In no way are any of the following rights affectedby the license:
– Your fair dealing or fair use rights, or other applicable copy-right exceptions and limitations;
– The author’s moral rights;– Rights other persons may have either in the work itself or
in how the work is used, such as publicity or privacy rights.
Notice For any reuse or distribution, you must make clear to others the licenseterms of this work. The best way to do this is with a link to this web page.