Table of contents
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.
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
# 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 ...
# List all files and directories, including hidden ones, in long format with human-readable file sizes ls -alth
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:
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
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
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
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
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
Here are a few advanced Vi commands that you might find useful:
:%s/old/new/g- Replace all occurrences of
newin the entire file
:%d- Delete all lines in the file
:r file- Insert the contents of
fileat the current cursor position
:w file- Save the current file as
: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
Now let's move on to some Git commands.
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.
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
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.
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"
This command is used to push local commits to a remote repository.
git push origin master
This command is used to retrieve updates from a remote repository and merge them into your local repository.
git pull origin master
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
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
This command is used to merge one branch into another.
git merge new_branch
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
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.
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.
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>
This command is used to show the commit history for the current branch.
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
Now that you've learned some essential
git, Linux, and
vi commands, try using them to complete the following tasks - ANSWERS FURTHER BELOW:
Create a new directory called "project"
Change into the "project" directory
Create a new file called "file1" in the "project" directory
Create a new file called "file2" in the "project" directory
List the files and directories in the "project" directory
Print the current working directory
Rename "file1" to "file1_renamed"
Copy "file2" to a new file called "file2_copy"
List all files and directories in the "project" directory, including hidden ones, in long format with human-readable file sizes
Open "file1_renamed" in the Vi editor
Enter insert mode and add some text to the file
Enter command mode and save and close the file
Initialize a new Git repository in the "project" directory
Clone an existing remote repository to the "project" directory
Add "file1_renamed" and "file2_copy" to the staging area
Commit the changes in the staging area
Push the local commits to a remote repository
Show the differences between the staging area and the working directory
Show the status of the working directory and the staging area
Temporarily save changes that are not ready to be committed
Undo the last commit and move the branch pointer to the previous commit
Show the commit history for the current branch
Show the commit history for the current branch in a single line per commit format
Create a new branch called "new_branch"
Switch to the "new_branch" branch
Merge the "new_branch" branch into the current branch
Download new commits from a remote repository
Download new commits from a remote repository and automatically merge them into the current branch
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.
mv file1 file1_renamed
cp file2 file2_copy
git add file1_renamed file2_copy
git commit -m "Initial commit"
git log --oneline
git branch new_branch
git checkout new_branch
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!