Git is a source code management system that allows for distributed version control and collaboration on projects. It provides commands for initializing and cloning repositories, tracking and committing file changes, reverting and resetting commits, working on branches, and connecting local repositories to remote servers for sharing code with others. Key features include strong support for non-linear development, speed, portability, and distributed collaboration on large projects.
3. What is Git?
• Source Code Management (SCM) system
• Designed for Linux Kernel development
• Free software as “Free Beer”
• Multiplatform
4. Git is not
• Another version of SVN
• Expensive
• A hacker’s tool to develop Linux kernel
• Hard to set up
• Hard to learn
• Complex
5. Why Git?
• Strong support for non-lineardevelopment
• Distributed development
• Efficient for large projects
• Faster than other SCM systems
• Very fast and portable (written in C)
6. 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
7. Configuring Git
• Adding your info:
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
git config --global color.ui true
• Git services (like GitHub) will identify you by
using this information
8. Some simple commands
• Initializinga local repository
git init [path/to/repo]
• Getting a remote repository
git clone https://user@service/path/to/repo.git
9. 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/
10. 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)
11. 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)
12. 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
14. 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
16. 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
17. 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.
18.
19. 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>
20. 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!
22. 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
24. 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)
25. 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
30. 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
32. 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>
34. 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
36. 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)
37. 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
38. 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 downloadand have the
same repo as you do
39. 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
40. 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
42. 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
43. Pulling from a remote repo
• Pulling = fetching + merging
• Useful commands:
# same as git fetch and then git merge
git pull <remote>
45. 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