Blogs


Basics GIT Interview Questions for DevOps



Q 1: Create a New Git Test Repo

Let's start our adventure by creating a new Git repository. Navigate to your desired directory and initialize a new repository using the following commands:

        
    $ mkdir git-test-repo
    $ cd git-test-repo $ git init
    

Q 2: Create a New Branch

Branches allow developers to work on different features or fixes independently. Let's create a new branch named "feature-wolf-count" using the command:

        
    $ git branch feature-wolf-count 
    $ git checkout feature-wolf-count 
    # or use the shorthand: git checkout -b feature-wolf-count
    

Q 3: Create a Simple Text File

Next, let's create a simple text file called "sample.txt" using your favorite text editor:

        $ touch sample.txt
    

Q 4: Create a Script to Count 'wolf'

Now, let's write a script in either shell or Python to search for the word 'wolf' and count its occurrences in each line of the text file. Here's a simple example in Python:

        
    # wolf_counter.py 
    with open('sample.txt', 'r') as file: 
    	for i, line in enumerate(file, start=1): 
        	count = line.lower().count('wolf') 
            	print(f"Line {i}: {count} occurrences of 'wolf'
    

Q 5: Add Files and Commit

Add both the text file and the script to your branch and commit the changes:

        
    $ git add sample.txt wolf_counter.py 
    $ git commit -m "Add text file and wolf counting script"
    

Q 6: Push the Branch to the Master

Now, push your branch to the master branch:

        
    $ git push origin feature-wolf-count
    

Q 7: Pull the Branch Again

Imagine that your collaborator has made some changes. Pull the latest updates from the master branch:

        
    $ git checkout master 
    $ git pull origin master 
    $ git checkout feature-wolf-count 
    $ git merge master
    

Q 8: Add Another Function to Count Lines

Enhance your script to count the number of lines in the text file. Modify the Python script as follows:

        
    # wolf_counter.py 
    with open('sample.txt', 'r') as file: 
    	line_count = sum(1 for line in file)
        print(f"Total lines in the file: {line_count}")
    

Q 9: Git Push the Changes

Add, commit, and push the changes to your branch:

        
    $ git add wolf_counter.py 
    $ git commit -m "Add line counting function" 
    $ git push origin feature-wolf-count
    

Q 10: Revert the Last Git Commit

If you need to undo the last commit, use the following commands:

        
    $ git log # to find the commit hash 
    $ git revert <commit-hash> 
    $ git push origin feature-wolf-count
    



Comments

Free Harvard Inspired Resume Template