source code management with git
TRANSCRIPT
What is Git?
• Source Code Management (SCM) system
• Designed for Linux Kernel development
• Free software as “Free Beer”
• Multiplatform
Git is not
• Another version of SVN
• Expensive
• A hacker’s tool to develop Linux kernel
• Hard to set up
• Hard to learn
• Complex
Why Git?
• Strong support for non-linear development
• Distributed development
• Efficient for large projects
• Faster than other SCM systems
• Very fast and portable (written in C)
Getting Git in RPi
• To install Git in Raspberry Pi image, execute sudo apt-get install git-core
• Now run git --version
• This will show the version of Git installed
Configuring Git
• Adding your info: git config --global user.name "John Doe"
git config --global user.email [email protected]
git config --global color.ui true
• Git services (like GitHub) will identify you by using this information
Some simple commands
• Initializing a local repository git init [path/to/repo]
• Getting a remote repository git clone https://user@service/path/to/repo.git
Playing with git
mkdir Desktop/learning_git
cd Desktop/learning_git
git init
• This will create a repository in a folder named learning_git, outputting this
Initialized empty Git repository in /home/rpi/Desktop/learning_git/.git/
Adding files to repo
• Add some text to a file called “hello.txt”:
nano hello.txt # opens hello.txt in nano editor
• Write something in the file just created and CTRL+X to exit from the editor (press Y to save changes in hello.txt)
Adding files to repo
git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# hello.txt
nothing added to commit but untracked files present (use "git add" to track)
Adding files to repo
git add "hello.txt"
• This will add “hello.txt” to your repository, making it ready for commit
• Now check the status again…
• This process (creating files then adding them) is called staging
Adding more files
• Create two other text files called “file1.txt” and “file2.txt”
• Check the status (git status)
• You can add all files to stage by executing one of these:
git add *.txt # adds only .txt files
git add . # adds every file
Commits (CTRL+S the project)
• Committing is just like saving the current stage in the history
• Committing helps you return in a previous stage of the project (in case you screw up everything)
• Execute: git commit -m "Initial Commit"
• Now check the status, again
Checking history
• Now create some other files and commit them.
• After committing again, execute git log to see all commits, along with the message for each of them.
CTRL+Z with git checkout
• Checking out (git checkout) does 3 things:
– Checks out files
git checkout <commit> <file>
– Checks out commits
git checkout <commit>
– Checks out branches
git checkout <branch>
CTRL+Z with git checkout
• To get the ID of every commit: git log --oneline
• Pick an ID and execute:
git checkout <commit>
• Now check the files on your repo; they are not the same as before!
CTRL+Z with git revert
• To get the ID of every commit: git log --oneline
• Pick an ID and execute:
git revert <commit>
• Reverting does not remove the commit from the history. Instead, it undoes the changes by that commit and creates a new commit with changes undone
CTRL+Z with git reset
• Dangerous version of git reverse
• Permanent undo: git reset is irreversible
• Often used (securely) to undo changes in staging area and/or working directory (i.e. delete changes not committed yet)
CTRL+Z with git reset
• Let’s assume there are 3 files in the local repo. Execute:
git add . # stage all files git reset file2 # remove file2 git commit -m "testing git reset" # commit changes # this will remove unstaged files i.e. file2 git clean -f
• Since file2 is removed using git reset, it won’t commit
Branches
• Independent workflows of development
• Mostly used to fix bugs and/or add features to a project
Branches
• Useful commands:
git branch # list branches
git branch <branch> # create new branch
git branch -d <branch> # delete <branch>
git branch -D <branch> # force delete <branch>
git branch -m <branch> # rename current branch
Branches
• Execute: git branch try_branches # create a new branch
git checkout try_branches # go to that branch
# create some files and commit them
git checkout master # main branch is master
# check files
# no one has changed
# branches are independent
Merge branches
• There are two types of merging:
– Fast Forward Merging
– 3 Way Merging
# this merges <branch> with the current branch
# merging type is decided by Git
git merge <branch>
Fast Forward Merging
• Make sure you are on master branch, and execute (after executing code on slide 30):
# this will merge try_branching to master
git merge try_branching
3 Way Merging
• Do:
1. Create a new branch (branch3WM)
2. Add a new file to master and commit it
3. Move to branch3WM and add some files and commit them
4. Move to master branch
5. Do the 3 Way Merging (git merge branch3WM)
3 Way Merging conflicts
• Conflicts happen when
– The same file is modified in 2 different branches
– 2 (or more) developers work on the same project simultaneously
• TODO for you:
– Simulate (and fix) a 3WM conflict
Remote repos
• Putting your code online makes it simpler for others to cooperate
• All files, branches and local repo history is available for others to download and have the same repo as you do
Working with connections
• Useful commands:
git remote # list all remote repos
git remote –v # list with url
git remote add <name> <url> # add a new remote repo
git remote rm <name> # remove a remote repo
git remote rename <old> <new> # rename a repo
Working with connections
• Execute this:
#
# This will add a remote repo named
# thl_lg_repo with
#
git remote add thl_lg_repo https://bitbucket.org/aziflaj/thl_learning_git.git
Fetching from a remote repo
• Useful commands:
git fetch <remote> # fetch all branches
git fetch <remote> <branch> # fetch only a branch
• After fetching, execute git merge to update your master branch
Pulling from a remote repo
• Pulling = fetching + merging
• Useful commands:
# same as git fetch and then git merge
git pull <remote>
Pushing to a remote repo
• Pushing is the opposite of fetching
• Transfers files from local to remote repo (overwrites changes)
• Useful commands:
git push <remote> <branch> # push a branch only
git push <remote> --all # push all branches
git push <remote> --force # force push