Automation QA Testing Course Content

What is Git, GitHub,SCM and CM

What is CM and what are diff SCM tool?
  • Configuration management(CM) is managing the configuration of all of the project's key products and assets.
  • SCM stands for Source Code Management is an integral part of any project in the IT world.
  • Important component in DeveOps culture. 
  • Source Code Management or Version Control Systems in any project ensure all the members of a team stay on top of the source code changes.
  • SCM practices include revision control and the establishment of baselines.
Top SCM Tools:
  • MS Team Foundation Server (TFS):
  • Kallithea - Open Source
  • GitLab - Continuous Integration (CI), Continuous Delivery (CD) is an integral part of GitLab
  • Bitbucket Server:
  • Subversion (SVN):
  • Git and GitHub
Why do we use version control System?
  • Collaboration- Without a SCM in place, you're probably working together in a shared folder on the same set of files. It's extremely error prone, someone will overwrite someone else's changes. With a SCM, everybody on the team is able to work absolutely freely - on any file at any time. 
  • Storing Versions
  • Restoring Previous Versions
  • Understanding What Happened: Every time you save a new version of your project, your SCM requires you to provide a short description of what was changed. This helps you understand how your project evolved between versions.
  • Backup
Git and GitHub
  • Git – initially developed by Linus Torvalds is a version control system.
  • Git is a version control system that lets you manage and keep track of your source code history. 
  • Git is free and open source
  • GitHub is a cloud-based hosting service that lets you manage Git repositories.
  • GitHub is used to do code reviews and maintain documentation
  • GitHub is used for Collaboration between team members

Why Git[Version Control]:
        1)Versioning a file is important
        2)Tracking changes and owner of changes
        3)Enables the team to collaborate and work efficiently



Different Terminologies with GitHub

Repository: You can simply, treat it as a storage area of your workplace that contains all your documentation files and the history of changes.
Clone: Clones are literally clones (copies) of a repository that sit on the developer’s computer instead of a server elsewhere.
Commit: Whatever the changes you make in your files will come under commit. Every change is saved under a particular name or ID which is also called “revision”.
Push: Pushing refers to sending your committed changes to a remote repository such as GitHub.com.

Pull Request: If you have made changes in code/script, to show the other collaborators you send a pull request.
Fork: It is a copy of other's repository in your account in which you can make changes and it won't affect the original code.
Branching: When you extract a portion /section of code from the main or remote track of your software, then it is called ‘branch' and the process is known as Branching.
Fetch: Fetching refers to getting the latest changes from an online repository (like GitHub.com) without merging them in.
Merge: Merging takes the changes from one branch (in the same repository or from a fork), and applies them into another.


Steps to work with Git and GiHub
  • Download and Install Git - https://git-scm.com/download/win , https://git-scm.com/download/mac
  • Create a GitHub Account
  • Using Command Prompt - by using different git commands
  • Using Eclipse -  we have inbuilt Git plugin in eclipse
Please refer below different git commands:
1. git config
This command sets the author name and email address respectively to be used with your commits.
git config global user.name [name]  
git config global user.email [email address]
Example: 
git config user.name "Ramesh Ch"
git config user.email "rameshn3@gmail.com"

2. git init
This command is used to start a new repository.
git init [repository path
Example: navigate to repo path and
git init

3. git clone
This command is used to obtain a repository from an existing URL.
git clone [url]  
Example: navigate to your repo path where you want to clone and write below command in cmd
git clone https://github.com/rameshn3/Test2

4. git add
This command adds a file to the staging area.
git add [file] 
Example: 
git add [file]
git add *  
git add .

5. git commit
git commit -m [commit message] 
This command records or snapshots the file permanently in the version history. 
Example:
git commit -m First Commit

6. git diff
This command shows the file differences which are not yet staged.
Example:
git diff -staged
git diff [first branch] [second branch]  

7. git reset
This command unstages the file, but it preserves the file contents.
git reset [file]  
git reset [commit]  
git reset hard [commit]

8. git status
git status  
This command lists all the files that have to be committed.

9. git rm
This command deletes the file from your working directory and stages the deletion.
git rm [file] 

10. git branch
git branch  
This command lists all the local branches in the current repository.
Example:
master
feature-test-payment

11. git log
git log  
This command is used to list the version history for the current branch.
Example:
git log --online

12. git merge
git merge [branch name]  
This command merges the specified branchs history into the current branch.

13. git remote add [variable name] [Remote Server Link]  
This command is used to connect your local repository to the remote server.
Example:
git remote add origin https://github.com/rameshn3/LinkedinHybridFramework-July222022.git

14. git push
git push [variable name] master  
This command sends the committed changes of master branch to your remote repository.
Example:
git push origin master
git push origin master --force

15. git pull
git pull [Repository Link]  
This command fetches and merges changes on the remote server to your working directory.
Example:
git pull https://github.com/rameshn3/LinkedinHybridFramework-July222022.git
16. git show git show [commit] This command shows the metadata and content changes of the specified commit. Command: git show <ChangeID>:<FilePath> Example: git show 45dhfg56:/src/test/newtest.xml 17. Checkout git checkout [branch name] This command is used to switch from one branch to another or You can get the specific previous version. Command: git checkout <ChangeID> <filePath with extenion> Example: git checkout 6475fgh5 pom.xml

Undo local changes

Until you push your changes to any remote repository, they will only affect you. That broadens your options on how to handle undoing them. Still, local changes can be on various stages and each stage has a different approach on how to tackle them.

Unstaged local changes (before you commit)

When a change is made, but it is not added to the staged tree, Git itself proposes a solution to discard changes to a certain file.

Suppose you edited a file to change the content using your favorite editor:

vim <file>

Since you did not git add <file> to staging, it should be under unstaged files (or untracked if file was created). You can confirm that with:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   <file>
no changes added to commit (use "git add" and/or "git commit -a")

At this point there are 3 options to undo the local changes you have:

  • Discard all local changes, but save them for possible re-use later:

    git stash
    
  • Discarding local changes (permanently) to a file:

    git checkout -- <file>
    
  • Discard all local changes to all files permanently:

    git reset --hard
    

Before executing git reset --hard, keep in mind that there is also a way to just temporary store the changes without committing them using git stash. This command resets the changes to all files, but it also saves them in case you would like to apply them at some later time.

How to Clone the repository to local machine

git clone <repository url> --clone the reposioty to local machine

git add . or git add <filename> add the file(s) to local repository

git commit -m"commit message" --save the changes to local repository

git push origin branchname --pushing local changes to remote branch

How to update/Refresh a Git branch

Assuming you’ve been working on a feature branch:
$ git branch
* master
$ git checkout -b feature
Switched to a new branch 'feature'
# make some commits...
You find out there are some new commits on master:
$ git checkout master
$ git pull
And you want to merge them to your feature branch.
How would you go about doing that?

Merge

The simplest approach is to merge the changes:
$ git checkout feature
$ git merge master
If there are differences, then merge will apply the commits to the top of feature and create a new merge commit. Otherwise, the merge will be resolved by a fast-forward.

Rebase

Alternatively, there’s rebase:
$ git checkout feature
$ git rebase master
Rebase, on the other hand, moves all diverging commits of feature to the top.
This means that the diverging commits will have new hashes because history will be rewritten.
Accordingly, if you’ve previously pushed your feature branch to remote, then the only way to update it is with force push:
$ git push origin feature --force
However, if developers have checked out your feature branch, then I don’t recommend this method. Stick with merge.


=====================================================
Issues faced during git commands:
1) git push origin opencarttestsmay1424
fatal: 'origin' does not appear to be a git repository fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
------
Solution:

This error indicates that the remote repository URL (origin) is not properly configured or accessible. Here's how you can troubleshoot and fix the issue:

  1. Check Remote URL Configuration: Make sure the remote URL is correctly set. Run the following command to see the current remote URL:

    sh
    git remote -v

    If you do not see a remote named origin or the URL is incorrect, you need to add or update it.

  2. Add/Update the Remote URL: If origin is not set or the URL is incorrect, you can set it using the following command:

    sh
    git remote add origin <URL>

    If origin is already set but with the wrong URL, you can update it with:

    sh
    git remote set-url origin <URL>

    Replace <URL> with the correct URL of your remote repository. It could be in the format of HTTPS or SSH, like:

    • HTTPS: https://github.com/username/repository.git
    • SSH: git@github.com:username/repository.git
  3. Verify Access Rights: Ensure you have the necessary permissions to access the remote repository. If you are using SSH, make sure your SSH keys are correctly set up. You can test SSH connection with:

    sh
    ssh -T git@github.com

    If you are using HTTPS, make sure you have the correct username and password (or access token) configured.

  4. Check Repository Existence: Ensure that the repository exists at the specified remote URL. You can do this by visiting the URL in your web browser.

  5. Try Cloning the Repository Again: If you are still having issues, you might want to try cloning the repository again in a new directory to see if the problem persists:

    sh
    git clone <URL> cd repository

After making sure the remote URL is correctly set and you have the necessary access rights, try pushing your changes again:

sh
git push origin opencarttestsmay1424

---------------------------------------------------------

Issue2)

ssh -T git@github.com The authenticity of host 'github.com (140.82.116.3)' can't be established. ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'github.com' (ED25519) to the list of known hosts. git@github.com: Permission denied (publickey).

------------------

Solution:

The error message you received indicates that GitHub's SSH key fingerprint is being added to your list of known hosts for the first time, which is normal. However, the Permission denied (publickey) error suggests that your SSH key is not being recognized or is not correctly configured. Here’s how to resolve this issue:

  1. Check for Existing SSH Keys: Verify that you have an SSH key pair already generated on your machine. You can check this by looking in the ~/.ssh directory:

    sh
    ls -al ~/.ssh

    Look for files named id_rsa and id_rsa.pub or id_ed25519 and id_ed25519.pub.

  2. Generate a New SSH Key (if needed): If you do not have an SSH key pair, you need to generate one. You can do this with:

    sh
    ssh-keygen -t ed25519 -C "your_email@example.com"

    If you prefer to use RSA, you can generate an RSA key pair instead:

    sh
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

    Follow the prompts to save the key and optionally add a passphrase.

  3. Add Your SSH Key to the SSH Agent: Start the SSH agent in the background and add your SSH key:

    sh
    eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519

    If you used an RSA key, replace id_ed25519 with id_rsa.

  4. Add the SSH Key to Your GitHub Account: Copy the contents of your public key to your clipboard:

    sh
    cat ~/.ssh/id_ed25519.pub

    Go to your GitHub account settings and navigate to SSH and GPG keys. Click New SSH key, give it a title, and paste your public key into the "Key" field. Save the new key.

  5. Test the SSH Connection: Test the SSH connection again to ensure everything is set up correctly:

    sh
    ssh -T git@github.com

    You should see a success message, such as:

    sh
    Hi username! You've successfully authenticated, but GitHub does not provide shell access.
  6. Check SSH Configuration (if needed): If you are still facing issues, you might need to check your SSH configuration file (~/.ssh/config). Ensure it includes the following configuration:

    sh
    Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519

    If you used RSA, replace id_ed25519 with id_rsa.

After completing these steps, try pushing your changes again:

sh
git push origin opencarttestsmay1424

This should resolve the permission issue and allow you to push to the remote repository.

=========================================================

if you face below issue While pushing your code to Github in gitbash

$ git push origin apirestassured To https://github.com/rameshn3/BddRestAssuredAPIAutomationMay212024.git ! [rejected] apirestassured -> apirestassured (non-fast-forward) error: failed to push some refs to 'https://github.com/rameshn3/BddRestAssuredAPIAutomationMay212024.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. If you want to integrate the remote changes, hint: use 'git pull' before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Solution for the issue is. Please follow the below steps:

The error message indicates that your local branch apirestassured is behind the remote branch. This usually happens when changes have been made to the remote branch that are not present in your local branch. To resolve this, you need to update your local branch with the changes from the remote branch before you can push your changes.

Here are the steps to resolve this issue:

  1. Fetch the Latest Changes: Fetch the latest changes from the remote repository.

    sh
    git fetch origin
  2. Merge the Changes: Merge the changes from the remote branch into your local branch. This can be done using git pull, which fetches and merges in one step.

    sh
    git pull origin apirestassured
  3. Resolve Any Merge Conflicts: If there are any merge conflicts, resolve them. After resolving the conflicts, add the resolved files to the staging area.

    sh
    git add <resolved-file>
  4. Commit the Merge: Commit the merge if necessary.

    sh
    git commit -m "Resolve merge conflicts"
  5. Push the Changes: Push your local changes to the remote branch.

    sh
    git push origin apirestassured

Step-by-Step Commands

  1. Fetch the latest changes:

    sh
    git fetch origin
  2. Pull the latest changes and merge:

    sh
    git pull origin apirestassured
  3. If there are merge conflicts, resolve them and add the resolved files:

    sh
    git add <resolved-file>
  4. Commit the merge if necessary:

    sh
    git commit -m "Resolve merge conflicts"
  5. Push your changes:

    sh
    git push origin apirestassured

Example Workflow

Here’s an example of the complete workflow:

sh
# Fetch the latest changes from the remote repository git fetch origin # Pull and merge the changes from the remote branch git pull origin apirestassured # If there are merge conflicts, resolve them manually, then add the resolved files # git add <resolved-file> # Commit the merge if necessary # git commit -m "Resolve merge conflicts" # Push the changes to the remote branch git push origin apirestassured

By following these steps, you should be able to resolve the issue and push your changes successfully to the remote branch.








No comments:

Post a Comment

Note: Only a member of this blog may post a comment.