Beware: commands contain small typos. You must fix them to properly complete the course!
The importance of tracking changes.
VCS basics:
In [ ]:
! mkdir -p /repo-path
!cp -v /etc/host* /etc/s* /repo-path
All operations are local to /repo-path
In [ ]:
cd /repo-path
Always timestamp backup copies, don't .ori
.
In [ ]:
! SIMPLE_BACKUP_SUFFIX="$(date -I)" cp -v -bf hosts hosts
!ls -l hosts*
Exercise: Use date +%s
to timestamp a backup copy of hosts
.
In [ ]:
# Use this cell for the exercise.
In [ ]:
!git config --global user.email "jon@example.com"
!git config --global user.name "Jon Doe"
Note: authentication can not be enforced on a local repository.
In [ ]:
!git init /repo-path
!ls -l /repo-path/.git # this is the index directory
In [ ]:
# Write here the command
# and show the git config file.
!cat .git/config
Enter in the repo directory and check the status: there are a lot of files we are not interested in...
In [ ]:
!git status
.gitignore
lists the files we're not interested in
In [ ]:
# Ignore all files not starting with h
!echo "[^h]*" >> .gitignore
!git status
Now we have all host*
files to be tracked.
In [ ]:
! git add hosts
The file is now staged for commit. It's not archived though.
In [ ]:
!git status
Save files to the local index
In [ ]:
! git commit -m "Initial snapshot of hosts"
In [ ]:
!echo "127.0.0.2 localhost2.localdomain" >> hosts
!git diff hosts
If we like the changes, we can stage them
In [ ]:
!git add hosts
!git status
and finally save them in the repo.
In [ ]:
!git commit "Added localhost2 to hosts"
In [ ]:
!git log
In [ ]:
! git checkout HEAD~1 -- hosts # revert hosts to the previous commit
In [ ]:
! mkdir -p /repo-path
!date >> /repo-path/file.txt
!date >> /repo-path/hi.txt
In [ ]:
!git init /repo-path # Initialize repo eventually creating a directory
!git add /repo-path/hi.txt # Add file to index
!git commit -m "My changes" # Save changes
In [ ]:
# Use this cell for the exercise
In [ ]:
!date >> /repo-path/file.txt
!git diff
!git commit -a -m "Save all previously added files"
In [ ]:
!git log /repo-path/file.txt # show changes
In [ ]:
!git checkout HEAD~1 -- file.txt # revert file
In [ ]:
!git diff HEAD # diff with reverted
In [ ]:
!git checkout HEAD -- . # get *all files* from the latest commit
In [ ]:
!git tag myconfig-v1 # create a tag
!git tag -l # list tags
In [ ]:
!git branch -a
Create a branch
In [ ]:
!git checkout -b work-on-my-changes
And list the branches, check the active one!
In [ ]:
!git branch -a
Modify a file in a branch
In [ ]:
!date > new-file.txt
!git add new-file.txt
With commit we consolidate the new file in the branch
In [ ]:
!git commit -m "Added a new file"
Compare branches
In [ ]:
!git diff mister
Diff supports some parameters
In [ ]:
!git diff --ignore-all-space master
We can now switch between branches
In [ ]:
!git checkout master
!cat new-file.txt
And switch back
In [ ]:
!git checkout work-on-my-changes
!cat new-file.txt
In [ ]:
# Use this cell for the exercise
In [ ]:
!date >> new-file.txt
!git checkout master
You have to remove the changes or commit them (in another branch too)
In [ ]:
# Use this cell for the exercise.
In [ ]:
!git checkout master
!git diff work-on-my-changes
!git merge work-on-my-changes
After a merge, if the branch is no more useful, we can remove it.
In [ ]:
!git branch -d work-on-changes
If there are unmerged changes, git doesn't allow deleting a branch.
Exercise:
git branch -d
to remove the antani
branch-d
with -D
. Does it work now?
In [ ]:
# use this cell for the exrcise
In [ ]:
!git add -p
In [ ]:
! mkdir -p /repo-tmp && cd /repo-tmp # use another directory
In [ ]:
! git clone https://github.com/ioggstream/python-course/ python-course
cd /repo-tmp/python-course
Show repository configuration. Remote origin.
In [ ]:
! git config -l
The remote repo is retrieved with all its changes and history
In [ ]:
! du -ms .git
And log
can show branches and merges.
In [ ]:
!git log --graph
In [ ]:
! git clone /repo-tmp/python-course /repo-tmp/my-course
Show repository configuration. Remote origin.
In [ ]:
! git config -l
In [ ]:
! git push origin test-1
Remember:
git config -l
test-1
is the branch name where you want to uploadTo upload changes to the remote master (default) branch, you need to
In [ ]:
!git checkout master
!git merge test-1
In [ ]:
!git push origin master
To make it work, you need to be authenticated/authorized with the remote repo ;)