Basic Commands
Goal
- This project is about the basic functionality of git. You will change/modify files.
- After every action run
git statusto see the results of your changes. In the end you'll create a commit with all your changes and send it to the server by pushing it.
Hints
- Try to solve the following tasks with the help of the Slides and the Cheatsheets.
- If you still run into problems, you'll find an expandable hint block for every task with a detailed description of the solution.
Work on the following tasks and run git status after every action that you take.
Task 1
- Change the content of
files/change_me.txtand save it. - Let git show you the changes that you made.
Solution (Click on the arrow if you are stuck)
- Open the file
change_me.txtin the file browser on the left side atmy-fist-project/files. - Change the content and save it with
Ctrl + s. - Click on the terminal and make sure that you are in the folder
~/workspace/my-first-project. - Run
git status. The output should includemodified: files/change_me.txt.
Task 2
- Move the file
files/move_me.txtto a different location. Use the respective git command for that. - Let git show you the changes that you made.
Solution (Click on the arrow if you are stuck)
- Run
git mv files/move_me.txt files/new_location.txt. - Run
git status. The file was renamed and added to the staging-area automatically.
Task 3
- Delete the file
files/delete_me.txt. - Let git show you the changes that you made.
Solution (Click on the arrow if you are stuck)
- In the file browser look for
delete_me.txt. - Delete the file by right clicking on it and pressing
Delete Permanently. - Run
git status. The file is shown asdeleted.
Task 4
- Rename the file
files/rename_me.txtwith the file browser on the left side of VSCode. - Let git show you the changes that you made. The old file should be marked as
deletedand the new one asuntracked. - Add both files to the staging area.
- Let git show you the changes again. Both files should now be shown in the
Changes to be committedarea with the statusrenamed.
Solution (Click on the arrow if you are stuck)
- In the file browser search for
rename_me.txt. - Rename the file by right clicking on it and pressing
Rename. - Switch to the terminal and run
git status. The old file should be shown asdeletedand the new one asuntracked. - Add both files to the staging area by running
git add files/rename_me.txtandgit add files/{new_name} - Check the resulting state again by running
git status. Both files should now be listed underChanges to be committedwith the statusrenamed.
Task 5
- Create a new file
files/new.txtand add it to the repository. - Let git show you the changes that you made.
Solution (Click on the arrow if you are stuck)
- Create a new file by right clicking and pressing
New Fileon the directoryfiles. - Name the file
new.txt. - Run
git status. The file will now be listed underUntracked files.
Task 6
- Create a commit with all changes that you made so far.
Solution (Click on the arrow if you are stuck)
- Add all changes that you made in the directory
filesto the staging area by runninggit add files. - With
git diff --stagedyou can show all the changes that will be part of the commit. Withqyou can exit. - Create a commit by running
git commit -m "My first commit".
Task 7
- View your new commit in the commit history.
- The name and the email address are currently automatically configured.
Solution (Click on the arrow if you are stuck)
- Run
git log. Latest commits are on top. Withqyou can exit. - The commit that you just created will be shown.
Task 8
- Change the git identity to use your name and arbitrary email address.
Solution (Click on the arrow if you are stuck)
- Change your name with
git config --global user.name <Name>. - Change your email address with
git config --global user.email arbitrary@example.com.
Task 9
- Create the file
files/ignored.txt. This file should not be added to any commit and also not shown bygit status.
Solution (Click on the arrow if you are stuck)
- Create a file
ignored.txtin the folderfiles. - Run
git status. The file will be shown underUntracked files. - Create the file
.gitignorein the foldermy-first-project. -
Add the following line to the
.gitignorefile:files/ignored.txt -
Run
git statusagain. The fileignored.txtshould no longer be listed.
Task 10
- Create a second commit. It should contain only the file
.gitignore. - When your run
git statusafterwards, no changes should be visible.
Solution (Click on the arrow if you are stuck)
- Add
.gitignoreto the staging area by runninggit add files. - Run
git diff --stagedto show what changes will be committed. - Run
git commit. Thenanoterminal editor opens up. - Enter a commit message like
"Add .gitignore". - Save with
Ctrl + oandEnter. - Leave the terminal editor with
Ctrl + x.
Task 11
- Check your commit history again. A new commit with your name as an author should be visible.
Solution (Click on the arrow if you are stuck)
- Run
git log. - The commit created by you will be shown. This should show your name as author.
Task 12
- Push your changes to the server. In Gitlab you can verify that your changes were successfully sent to the server.
Solution (Click on the arrow if you are stuck)
- Run
git push. - Switch to the Gitlab web interface.
- On the top left corner under
Projectslook formy-first-project. - If
my-first-projectis not shown directly, click onYour projectsand you will find it there. - View the commit.