6. Linux, Vi, and Git Commands

6. Linux, Vi, and Git Commands

As a DevOps engineer, you'll be working with Git to manage code repositories and track changes. In this blog post, we'll go over some of the most essential git commands that you should know, commands to help you navigate Linux operating systems, and some vi commands to help you edit texts within Linux.

Linux Commands

Before we dive into Git, let's review some basic and advanced Linux commands that you'll need to know to navigate the terminal.

File and Directory Management

# Create a new directory
mkdir new_directory

# Create a new file
touch new_file

# Rename a file
mv old_name new_name

# Rename a directory
mv old_directory_name new_directory_name

# Delete a file
rm file1

# Delete an empty directory
rmdir directory1

# Delete a non-empty directory (use with caution!)
rm -rf directory1

Navigation

# List the files and directories in the current directory
ls

# Print the current working directory
pwd

# Change to a different directory
cd /path/to/directory

# Change to the home directory
cd ~

# Change to the parent directory
cd ..

# Change to the grandparent directory (one level up from the parent)
cd ...

Advanced Listing

# List all files and directories, including hidden ones, in long format with human-readable file sizes
ls -alth

Vi Editor

Vi is a powerful text editor that is included with most Linux distributions. Here are some basic commands for using Vi:

Opening and closing files

To open a file in Vi, use the vi command followed by the name of the file:

vi filename

To save and close the file, first press the Esc key to enter command mode, then type :wq and press Enter. To close the file without saving, type :q! and press Enter.

Editing text

To enter insert mode and begin editing text, press the i key. Press the Esc key to return to command mode.

To delete a single character, move the cursor over the character and press the x key. To delete an entire line of text, move the cursor to the beginning of the line and press the dd keys.

To copy a line of text, move the cursor to the beginning of the line and press the yy keys. To paste the copied text, move the cursor to the desired location and press the p key.

Searching

To search for a specific word or phrase in the file, press the / key, type the search term, and press Enter. To move to the next occurrence of the search term, press the n key. To move to the previous occurrence, press the N key.

To search for a word under the cursor, press the * key. To move to the next occurrence, press n, and to move to the previous occurrence, press N.

Advanced commands

Here are a few advanced Vi commands that you might find useful:

  • :%s/old/new/g - Replace all occurrences of old with new in the entire file

  • :%d - Delete all lines in the file

  • :r file - Insert the contents of file at the current cursor position

  • :w file - Save the current file as file

  • :set nu - Display line numbers

  • :set nonu - Hide line numbers

  • :set hlsearch - Highlight all occurrences of the current search term

  • :set nohlsearch - Disable search term highlighting

Git Commands

Now let's move on to some Git commands.

git init

This command is used to initialize a new Git repository. Run this command in the root directory of the project you want to track with Git.

git init

git clone

This command is used to create a local copy of a remote repository. Use it to pull down a copy of an existing repository from a remote server.

git clone https://github.com/user/repo.git

git add

This command is used to add new files or changes to files to the staging area.

git add file1 file2

You can also use the git add . command to add all new and modified files to the staging area.

git commit

This command is used to save changes to the local repository. You should include a commit message to describe the changes you are committing.

git commit -m "Added new feature"

git push

This command is used to push local commits to a remote repository.

git push origin master

git pull

This command is used to retrieve updates from a remote repository and merge them into your local repository.

git pull origin master

git branch

This command is used to create, list, or delete branches.

# Create a new branch
git branch new_branch

# List all branches
git branch

# Delete a branch
git branch -d old_branch

git checkout

This command is used to switch between branches.

# Create a new branch and switch to it
git checkout -b newer_branch

# Switch to a new branch
git checkout new_branch

git merge

This command is used to merge one branch into another.

git merge new_branch

git diff

This command is used to show differences between commits, the staging area, and the working directory.

# Show differences between the staging area and the working directory
git diff

# Show differences between the latest commit and the staging area
git diff --staged

# Show differences between two commits
git diff commit1 commit2

git status

This command is used to show the status of the working directory and the staging area. It will show which files are modified, which are in the staging area, and which are untracked.

git status

git stash

This command is used to temporarily save changes that are not ready to be committed. This can be useful when you need to switch branches, but you don't want to commit your changes yet.

git stash

git reset

This command is used to undo commits and move the branch pointer to a previous commit.

# Undo the last commit and move the branch pointer to the previous commit
git reset HEAD~

# Undo commits and move the branch pointer to a specific commit
git reset <commit>

git log

This command is used to show the commit history for the current branch.

git log

git log --oneline

This command is used to show the commit history for the current branch in a single line per commit format.

git log --oneline

Practice Tasks

Now that you've learned some essential git, Linux, and vi commands, try using them to complete the following tasks - ANSWERS FURTHER BELOW:

  1. Create a new directory called "project"

  2. Change into the "project" directory

  3. Create a new file called "file1" in the "project" directory

  4. Create a new file called "file2" in the "project" directory

  5. List the files and directories in the "project" directory

  6. Print the current working directory

  7. Rename "file1" to "file1_renamed"

  8. Copy "file2" to a new file called "file2_copy"

  9. List all files and directories in the "project" directory, including hidden ones, in long format with human-readable file sizes

  10. Open "file1_renamed" in the Vi editor

  11. Enter insert mode and add some text to the file

  12. Enter command mode and save and close the file

  13. Initialize a new Git repository in the "project" directory

  14. Clone an existing remote repository to the "project" directory

  15. Add "file1_renamed" and "file2_copy" to the staging area

  16. Commit the changes in the staging area

  17. Push the local commits to a remote repository

  18. Show the differences between the staging area and the working directory

  19. Show the status of the working directory and the staging area

  20. Temporarily save changes that are not ready to be committed

  21. Undo the last commit and move the branch pointer to the previous commit

  22. Show the commit history for the current branch

  23. Show the commit history for the current branch in a single line per commit format

  24. Create a new branch called "new_branch"

  25. Switch to the "new_branch" branch

  26. Merge the "new_branch" branch into the current branch

  27. Download new commits from a remote repository

  28. Download new commits from a remote repository and automatically merge them into the current branch

  29. Apply the commits from one branch on top of another branch

With these tasks, you'll get some hands-on experience using Git, Linux, and vi commands you've learned.

Solutions

  1. mkdir project

  2. cd project

  3. touch file1

  4. touch file2

  5. ls

  6. pwd

  7. mv file1 file1_renamed

  8. cp file2 file2_copy

  9. ls -alth

  10. vi file1_renamed

  11. i

  12. :wq

  13. git init

  14. git clone

  15. git add file1_renamed file2_copy

  16. git commit -m "Initial commit"

  17. git push

  18. git diff

  19. git status

  20. git stash

  21. git reset

  22. git log

  23. git log --oneline

  24. git branch new_branch

  25. git checkout new_branch

  26. git merge

  27. git fetch

  28. git pull

  29. git rebase

Conclusion

We've covered some of the most essential commands for DevOps engineers. With these commands in your toolkit, you'll be able to manage code repositories, track changes effectively, know your way around a Linux terminal and use vi to edit your files.

Remember to sign up for my newsletter, sign up for the AWS training session by donating any amount, follow me on Twitter, and stay tuned for more DevOps skills!


Did you find this article valuable?

Support Bilal Shafiq by becoming a sponsor. Any amount is appreciated!