Your ultimate guide

Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts
Git-Bash Setup & Git Commands

Git-Bash Setup & Git Commands



Git Bash:
    Git Bash is a command-line interface and shell environment that allows users to interact with Git, which is a version control system used for tracking changes in source code during software development. 
    Git Bash is one of the tools developers can use to interact with GitHub repositories through Git Command.




Git Bash installation:
  • Download the Git Bash setup based on your Operating System from the official website: "https://git-scm.com/downloads"
  • Run the .exe file you downloaded and follow the instructions in the installer.
  • Run Git Bash by right-clicking on any folder and selecting the Git Bash Here option from the context menu(right-click menu) or Search for the Git Bash app on the Start menu, then open it. 




Git Bash Setup:
Step 1: Setup User Name and Email ID
When we open Git Bash for the first time we should set up a user name and email ID details in Git Bash. For that enter the following command.

git config --global user.name "FIRST_NAME LAST_NAME"

Example:
git config --global user.name "Testing Colleges"
The user name does not need to same as your GitHub account user name.

After running the command if you want to change the user name then run the same command again it will replace the existing name.

git config --global user.email "YOUR_EMAIL"

Example:
git config --global user.email "email@example.com"
Provide only your email ID associated with your GitHub account.

To check the configuration details enter the following command:
git config --global –list

Step 2: Create a  Git Local Repository
    A Git local repository is a local copy of a Git repository. It is stored in a directory on your computer and contains all of the files and directories that are being tracked by Git. The Git local repository is used to manage changes to your code, such as adding new files, deleting files, and editing files. It is also used to track the history of changes to your code.
    By using the git init command we can create a new Git repository in Git Bash. This would create a new .git directory in the current directory, which would contain all of the necessary Git metadata for the new repository.

Here is an example of how you could use Git Bash to create a new Git repository.
Open Git Bash and Navigate to the directory where you want to create the Git repository, to navigate to the specific directory we will use the "cd" command (where "cd" stands for "change directory")
cd [absoult path of a directory]

Example:
cd E:\PythonPrograms
Type the following command:
git init

Once we create a git repository it will create three logical areas:
  1. Working Area/Untracked Files
  2. Staging Area/Tracked Files
  3. Local Repository
Working Area/Untracked Files:
  1. Whatever files are created in the git repository are automatically stored in this Working area
  2. For example, we are creating a file “DBConnect.py” by using the “touch DBConnect.py” command.
  3. To know the status of the file, and in which area the file is located use the “git status” command, if the file is in Red color then that file is in the Working Area.
Staging Area/Tracked Files
    After the file is completed in the working Area we are going to send that file into Staging Area. For that, we will use the following commands:
  1. git add .  : To send all files from the working area to the staging area.
  2. git add [filename1] [filename2]: To send specific files from the working area to the staging area. 
    • E.g.: git add DBConnect.py Index.sh
  3. git add *.<extension>: To send specific extension files from the working area to the staging area.
    • E.g.: git add *.py
    To know the status of the files, and in which area it is located use the “git status” command, if the files are in Green colour then those files are in the Staging Area.

Local Repository
    To commit the files to the Staging Area to the Local repository we will use the following commands.
  1. git commit –m “message”: To send all files from the Staging area to the Local repository. Eg: #git commit –m “First commit”
  2. git commit –m “message” [filename1] [filename2]: To send a specific file/s from the Staging area to the Local repository. E.g.: git commit -m "python files" DBConnect.py
    To know the status of the file, and in which area it is located use the “git status” command, if there are no files and displays “nothing to commit, working tree clean” then those files are in the Local repository area.


Step 3: Mapping Remote Repository to Local Repository
  • To connect GitHub to the Local repository, Go to your repository in your GitHub account.
  • Then select the "Code" option and copy the "HTTPS" URL


  • Now open the Git Bash application, and enter the following command to map the remote repository to the local repository.
git remote add [alias name] [https:// URL]
Here alias name is any local name that is used to refer to a remote repository.

Example:
git remote add tcp https://github.com/TestingColleges/python.git

To check which URL is mapped the command is ”git remote –v”


Step 4: Push the files from the Local Repository to the Remote Repository:
  • Go to git bash and enter the following command
git push [alias name] [branch name]

Example:
git push tcp master
  • Here master/main is a default branch name.
  • When you execute the command mentioned above, it will prompt you to enter your GitHub login details.
    • Enter the username of your GitHub account.
    • To access certain features, we require the use of a Personal Access Token (PAT) ID instead of a traditional password.
    • To obtain a PAT, navigate to GitHub, click on your profile, select Settings, then navigate to Developer settings and click on Personal access token. Finally, click on Generate new token.
    • Provide the token name, select its expiry date and select access scopes for personal tokens.
    • Then click on Generate Token
    • Now we get the PAT token, Copy the token & save it somewhere, because later won’t not able to see it again.
    • Past the PAT token in Password and hit the Enter key. Then it will push the files from the Local repository to the Remote repository.
  • The Next time, it will not be going to ask for the credentials, these credentials are stored in the Credential Manager application in Windows, and Keychain Access in Mac if you want you can delete these Credentials there



So Git Flow is: git init → git status → git add → git commit → git remote add → git push

Git and GitHub:
  • Git is a Version Control System, that is used to develop the codes and main the versioning.
  • GitHub is a Hosting service, that can be used to manage the source code, it will provide the GUI.
Difference between Git and SVN:
    Git is a Distributed Version Control System (DVCS). In this first we work on a Local repository then we distribute the code into a Remote Repository.
    SVN, CVS, and TFS are only Version Control Systems (VCS). In this, we will directly work on the Remote Repository.

Git + SVN = No
SVN client + SVN = Yes
Git + GitHub = Yes
Git + GitLab = Yes
Git + BitBucket = Yes
Git + Azure repos = Yes

What is a Version Control System?
    Developers make constant changes to code while creating an application, even after the official release. Version control systems help keep track of these changes by storing them in a central repository. This makes it easy for developers to collaborate on a project by downloading and uploading new revisions. By doing so, they can share the latest version of software with other developers, who can then make changes and contribute.


Note: After committing, if you modify the file code in the Local repository, then that modified file is stored in the working area.

We can send the modified file directly into the Local repository using the following command:
git commit -a -m “message”
Only this command works for modified files

git log: It will give all commit ids with detailed information.
git log -2: It will display the last 2 commit ids.
git log --oneline: It will give all commit ids without detailed information

git show [commit ID]: It will give details of this commit ID (we can get the commit ID in the “git log” command). E.g.: git show abc12345
git show --pretty="" --name-only  [Commit ID]: It will display all the files that are committed in that particular commit. E.g.: git show --pretty="" --name-only abc12345

To remove the working area file:
git clean -n: It will preview the changes that we are going to remove.
git clean -f: If we want to remove new files from the working area (Deleted data cannot be recovered).

To get back the file from the Staging area to the Working area:
git reset: To revert back all files to working from the staging area.
git reset [File Name]: To revert back the specific file to working from the staging area.

git revert [Commit ID]: It will revert the changes committed in that particular commit ID from the local repo.
git push origin master -f: It will revert the changes from the remote repo.
About GitHub & Setup

About GitHub & Setup


GitHub:
    GitHub is a web-based code hosting platform for software development and version control using Git. Git is a distributed version control system that helps developers manage and track changes in their code during the development process.
    GitHub allows developers to store their code repositories, collaborate with other team members or contributors, and manage projects efficiently. It provides features such as issue tracking, pull requests, code review, wikis, and actions for continuous integration and deployment.

    Similarly, like GitHub, there are other Source code Management(SVM) Tools are there like GitLab, Bit Bucket Server, SVN(Apache Sub Version), CVS(Concurrent Version System), TFS(Team Foundation Server), and so on.
* In Azure they have their own Source Code Management Tool Called Azure Repos.

Here are some key features and functionalities of GitHub:
  1. Repository: A repository (repo) is a storage space where a project's code and related files are stored. It's a central place for collaboration and version control.
  2. Version Control: GitHub uses Git for version control, enabling developers to track changes to their code, work on different branches, merge changes, and roll back to previous versions if needed.
  3. Collaboration: GitHub allows multiple developers to work on the same project simultaneously. Developers can create branches to work on specific features or fixes and merge them back into the main branch (often called "master" or "main").
  4. Issues and Pull Requests: Users can report issues or bugs in a repository, and developers can address them by creating pull requests with proposed code changes. Pull requests allow for code review and discussion before merging changes into the main branch.
  5. Code Review: GitHub facilitates collaborative code review, enabling team members to review proposed changes, comment on code, suggest improvements, and approve or request further modifications.
  6. Actions: GitHub Actions automates software development workflows, including build, test, and deployment processes. Users can create custom workflows or use pre-defined Actions to streamline their development workflow.
  7. Wikis and Documentation: GitHub provides a wiki feature for creating and maintaining project documentation, which is useful for explaining project structure, processes, or usage.
  8. Community and Social Interaction: GitHub has a strong community of developers who contribute to open-source projects, share code, collaborate, and discuss various topics related to software development.




GitHub Setup
Steps to Setup GitHub:
  1. To begin, the first thing you should do is create a GitHub account.
  2. Afterwards, you can create an organization within your account.
  3. After setting up the organization, you can then create a repository for your project.
  4. Once the repository is created, you can create teams within your organization.
  5. As needed, you can add members to each team.
  6. Lastly, you should grant the appropriate team access to the repository so they can contribute to your project.

Step 1: Create a GitHub account
  • Go to GitHub's official website. https://github.com
  • Click on the Sign up button.
  • Then on the next page give your personal details:
    • Email Id:
    • Create Password:
    • Enter a username:
    • Verify your account : (Please solve the simple puzzle to verify that you are human)
    • Then Click on the Create account Button.
  • Verify the email by entering the code: You'll receive an email with a code(or OTP) from GitHub. Enter that code on this verification page, then click Continue.
  • GitHub displays a quick survey, you can also skip this survey by clicking Skip personalization.
  • On the plan selection page, choose the free plan.

Step 2: Create Organization
    Within the organization, we can create repositories.
  • To create an Organization, first, you need to log in to your GitHub Account.
  • Then on the top right corner, we will see the "+" symbol, Click on that one.
  • Then select New Organization.

  • Then we will see three different plans (1. Free, 2. Team, 3. Enterprise)


  • Click on Free (Create a free Organization)
  • On the Next page, Setup your organization details:
    • Organization Account name* : <Give your organization name>
    • Contact email*: <Enter your email id>
    • This organization belong to: <Select "My personal account">
    • Verify your account by solving the simple puzzle.
    • Click on Next.
    • Then Click on Complete Setup.

Step 3: Create a Repository
   A repository is a location where we store all the source code and revise each file history.
  • In your GitHub account, in the top right corner, we will see the "+" symbol, Click on that one.
  • Select New repository.



  • Fill in all the details.
    • Owner*: < Select your Organization in Dropdown >
    • Repository name*: < Give the repository Name >
    • Description (optional): < Optional >
    • Select one option either Public or Private
      • Public < Selecting this option for Anyone on the internet can see this repository. You choose who can commit. >
      • Private < Select this option for You choose who can see and commit to the repository >


    • Initialize this repository with: < Don't select any option >
    • Click on the Create repository button.
    • Like this, we can create any number of repositories in a single organization.

Step 4: Create a Team
    A Team is a group of individuals (GitHub users) brought together within an organization to collaborate on repositories and projects more effectively. Teams help in organizing members, controlling access, and managing permissions within an organisation.
  • Open your Organization(click on your profile, then click on Your Organizations, Select your Organizations).
  • Then click on the Teams option.
  • Then Click on New team.


  • Fill in all details:
    • Team name: < Give the name of your team >
    • Description: < Optional  >
    • Team visibility: Select one option. The Team can be visible or secret
      • Visible: < Visible teams can be viewed and @mentioned  by every organization member.>
      • Secret: < Secret teams are only visible to the people on the team and people with owner permissions.>
    • Then click on Create team.

Step 5: Add Users to the Team
  • Open your Organization and click on 👥Teams.
  • Then click on your Team.
  • Then Select the Members option and Select the Add a member button.
  • Invite the Users by username, full name, or email address

  • After Creating a team we can also give the individual role to the users like Maintainer, Member
    • Maintainer: Can add and remove team members and create child teams.
    • Member: Has no administrative permissions on the team.

Step 6: Provide Repository Access to team
  • Open your team, then Click on Repositories.
  • Then click on the Add repository button, and type your repository name that we going to access to the team.



  • Then click on Add repository to team

  • After Providing repository access to the team, we can give the Permission level to access the repository by default it will be Read permission. If you want we can change the permission level.
    • Permission Level:
      • Read: Can read and clone this repository. Can also open and comment on issues and pull requests.
      • Triage: Can read and clone this repository. Can also manage issues and pull requests.
      • Write: Can read, clone. and push to this repository. Can also manage issues and pull requests.
      • Maintain: Can read, clone and push to this repository. They can also manage issues, pull requests, and some repository settings.
      • Admin: Can read, clone, and push to this repository. Can also manage issues, pull requests, and repository settings, including adding collaborators.



  • Just click on the permission you want it will apply to that team.