Your ultimate guide

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.

No comments:

Post a Comment