Day 13 Task: Advance Git & GitHub for DevOps Engineers

Day 13 Task: Advance Git & GitHub for DevOps Engineers

Git Branching

Branches are a core concept in Git that allow you to isolate development work without affecting other parts of your repository. Each repository has one default branch, and can have multiple other branches. You can merge a branch into another branch using a pull request.

Branches let you develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.

Registering Username and Email with Git on Ubuntu

To use Git effectively, you need to configure your username and email. This information will be associated with your commits.

Set Your Global Username & Global Email:

To check the status of your Git repository, you can use the git status command. This command provides information about the current state of your repository, including which files are staged for commit, which files have changes that are not staged, and which files are untracked.

Run the Git Status Command:

Key Concepts of Branching

  1. Main Branch:

    • The default branch in many Git repositories is called main (or sometimes master). This branch is often considered the stable version of your project.
  2. Feature Branches:

    • When developing new features or fixing bugs, it’s common to create a new branch specifically for that purpose. This is known as a feature branch (e.g., feature/new-login, bugfix/fix-typo).
  3. Branching Strategy:

    • Many teams use specific strategies for branching, such as Git Flow, GitHub Flow, or Trunk-Based Development, to manage the development process effectively.

How to Work with Branches

1. Create a New Branch

You can create a new branch using the following command:

2. Switch to a Branch

To switch to another branch, use:

Or, starting from Git 2.23, you can use the more intuitive command:

3. View Branches

To see a list of all branches in your repository, use:

What is push in Git?

The push command in Git is used to upload your local repository changes to a remote repository. When you make changes in your local repository (such as committing new files or modifying existing ones), those changes are only saved locally. To share your work with others or make it available on a remote server (like GitHub, GitLab, or Bitbucket), you use the git push command.

Key Concepts of git push

  1. Remote Repository: A version of your repository that is hosted on a server, accessible to others. Common remote repositories include GitHub and GitLab.

  2. Tracking Branch: When you push a branch to a remote repository for the first time, it establishes a tracking relationship between your local branch and the remote branch, making future pushes and pulls easier.

How to Use git push

Here are the steps to push your changes to a remote repository:

The command git remote -v is used to display the remote repositories associated with your local Git repository. This command provides a list of the remotes, showing their names and corresponding URLs for both fetching and pushing.

Understanding git remote -v

  • git remote: This part of the command is used to manage remote repositories. It allows you to view, add, remove, or rename remotes.

  • -v: This flag stands for "verbose." When you include it, the command will show the URLs associated with each remote, indicating how you can fetch from and push to those repositories.

  • Run the Command:

Using Personal Access Tokens in Git Push

  1. Generate a Personal Access Token:

    • For platforms like GitHub, GitLab, or Bitbucket, you can generate a PAT in your account settings under Developer settings or API settings. Make sure to give it appropriate permissions (like repo access).
  2. Format the URL:

    • Instead of using your username and password, you can include the token directly in the URL. The format is as follows:

After apply push command we can see the created README.md file below:

Created new file name “newtestfile.md” and committed it.

After, push command we can see the file" “newtestfile.md

Command to pull:

Output:

Git Commands Summary

  1. Push (git push):

    • Purpose: Upload local changes to a remote repository.

    • Usage: git push origin branch-name

    • Key Point: Shares your commits with others. 📤

  2. Merge (git merge):

    • Purpose: Combine changes from one branch into another.

    • Usage: git merge branch-name

    • Key Point: Integrates different lines of development. 🔗

  3. Status (git status):

    • Purpose: Show the current state of the repository.

    • Usage: git status

    • Key Point: Provides information on staged, unstaged, and untracked files. 📝

  4. Pull (git pull):

    • Purpose: Fetch and integrate changes from a remote repository into your local branch.

    • Usage: git pull origin branch-name

    • Key Point: Updates your local repository with the latest changes. ⬇️

  5. Rebase (git rebase):

    • Purpose: Reapply commits from one branch onto another, effectively changing the base of the branch.

    • Usage: git rebase branch-name

    • Key Point: Creates a cleaner project history by avoiding unnecessary merge commits. 🔄

  6. Revert (git revert):

    • Purpose: Create a new commit that undoes the changes made in a previous commit.

    • Usage: git revert commit-id

    • Key Point: Safely removes changes without altering history. ↩️

Conclusion

These commands form the backbone of Git workflow, allowing you to manage code changes, collaborate with others, and maintain a clear project history. Using them effectively can streamline your development process!

Happy coding! 🎉💻✨