In this tutorial, you will learn to install, configure, and use Git on Linux.
Part 1 – Launch terminal
In this part you will launch the Linux terminal.
1. Open the Terminal window.
Alternatively, you can press Ctrl+Alt+T to access the terminal.
Part 2 – Install Git
1. Update APT repository
sudo apt-get update
Enter password, if prompted
2. Install git.
sudo apt-get install git
Press Y, if prompted
3. In the terminal run following command to find git version number.
git –version
git help
git help -a
Part 3 – Using Git
In this part you will use Git to perform various operations, such as, check in, check status etc.
1. Switch to the “Documents” directory.
cd ~/Documents
2. Create a directory.
mkdir -p workspace/git
3. Switch to the “git” directory.
cd workspace/git
4. Initialize repository.
git init
5. Tell Git who you are.
git config --global user.name "Alice Smith"
git config --global user.email alice@smith.com
Note: One interesting aspect of Git is that it separates user identity in the repository from any sort of authentication or authorization. Because a distributed repository will generally be maintained by many separate individuals or systems, the identity of the committer must be contained in the repository – it can’t just be supplied as a user id when we do the commit. So, even if we’re not connected to any central repository, we need to tell Git who we are. The identity that we supply will be recorded whenever we commit to a repository.
6. Create a text file.
nano sample.txt
7. Enter following text.
First Version!
8. Press Ctrl+O to save the file and hit enter.
9. Press Ctrl+X to exit to the terminal.
10. Get Git status.
git status
Notice sample.txt is listed under untracked files.
11. Add the files to tracked.
git add .
Note: Here you are adding the current directory. You could also add the file using “git sample.txt”.
12. Get Git status again.
git status
Notice sample.txt is tracked.
13. Commit changes.
git commit
Notice it launched text editor automatically which lists operations that will get performed when files are committed. Here you can add detailed description that will get saved when you commit the changes.
14. Add following text in the first line.
Added sample.txt
15. Press Ctrl+O to save the file and hit enter.
16. Press Ctrl+X to exit to the terminal.
17. Get Git status.
git status
Notice it says there’s nothing to commit since you have already committed all changes.
18. Modify sample.txt.
nano sample.txt
19. Change “First Version!” to “Second Version!”
20. Press Ctrl+O to save the file and hit enter.
21. Press Ctrl+X to exit to the terminal.
22. Get Git status.
git status
Notice it says the file is modified.
23. View changes.
git diff
Notice it shows old text in red and new text in green.
24. Create another file.
nano another.txt
25. Add the following text.
Hello World!
26. Press Ctrl+O to save the file and hit enter.
27. Press Ctrl+X to exit to the terminal.
28. Add all files.
git add .
29. Get Git status.
git status
Notice it’s showing 1 file as modified and 1 file as a newly added file.
30. Commit changes.
git commit -m "Made 2 changes"
Notice when you pass -m switch, you can store a simple single line comment.
31. Get Git status.
git status
Notice there’s nothing to commit.
32. Delete sample.txt
rm sample.txt
33. Recover file.
git checkout sample.txt
34. View sample.txt
cat sample.txt
Note: It restored latest version by default.
35. Delete file again.
rm sample.txt
36. View all versions of a file.
git log
Notice it shows user, commit id, date time, and comment.
37. Copy the commit id for the older version.
38. View changes between current and the first version.
git diff <commit_id>
39. Restore the older version.
git checkout <commit_id> sample.txt
40. View file content.
cat sample.txt
Notice it’s the first version.
41. Close the terminal.
Part 4 – Review
In this tutorial, you installed and used Git.