Skip to main content

Basic Git Commands : Learn how to use Git.

Git is a popular version control system that allows you to track changes to your codebase, collaborate with other developers, and manage your code history. Here are some important Git commands and a brief guide on how to use Git:   

Getting started with Git

  1. Install Git: Download and install Git from the official website for your platform (Windows, macOS, or Linux).

  2. Configure Git: Once you have installed Git, you should configure your name and email address in Git by running the following commands in your terminal:


    git config --global user.name "Your Name"                                       git config --global user.email "youremail@example.com"

Creating a Git repository
To start using Git with your project, you need to create a Git repository. A Git repository is a folder that contains your project files and the Git configuration files. Here's how you can create a new Git repository:

  1. Create a new folder for your project:


    mkdir my-project cd my-project
  2. Initialize a new Git repository:

    git init


Git commands

Here are some of the most common Git commands you'll need to know:

git init: Initialize a new Git repository in your project folder.

git clone: Clone a remote Git repository to your local machine.

git add: Add files to the Git staging area. Use git add . to add all files.

git commit: Commit changes to the repository with a commit message describing the changes.

git push: Push changes from your local repository to a remote repository.

git pull: Pull changes from a remote repository to your local repository.

git status: Check the status of your repository, including which files have been modified or added.

git log: View the commit history of your repository.

git branch: Create or list branches in your repository.

git checkout: Switch between branches or checkout a specific commit.


Git workflow

Here's a brief overview of the basic Git workflow:

  1. Create a new branch: Before you start making changes to your code, create a new branch to work on.


    git checkout -b my-new-feature
  2. Make changes: Make changes to your code and add them to the staging area with git add.

    git add myfile.py
  3. Commit changes: Commit your changes with a descriptive message

    git commit -m "Added new feature"
  4. Push changes: Push your changes to the remote repository.

    git push origin my-new-feature
  5. Create a pull request: Once you have pushed your changes to the remote repository, create a pull request to merge your changes into the main branch.


    # on the remote repository                                                   git pull-request
  6. Review and merge: Review the changes in the pull request and merge them into the main branch.

Git branching

Git branching is a powerful feature that allows you to work on multiple versions of your code at the same time. You can create a new branch for a specific feature or bug fix, and merge it back into the main branch once it's complete. Here are some common Git branching commands:

git branch: List all branches in the repository. git checkout -b branch-name: Create a new branch and switch to it. git checkout branch-name: Switch to an existing branch. git merge branch-name: Merge changes from the specified branch into the current branch. git branch -d branch-name: Delete a branch.


To switch to a different branch, use the git checkout command:

git checkout feature-login

This will switch you to the feature-login branch.


To create a new branch we also use the git branch command:

git branch feature-login

This will create a new branch called feature-login

Git merging

Merging is the process of combining changes from one branch into another branch. There are two main types of merges in Git: fast-forward and recursive. A fast-forward merge occurs when the changes in one branch can be applied directly to the other branch without any conflicts. A recursive merge occurs when there are conflicts that need to be resolved before the changes can be merged. Here's how you can merge changes from one branch into another:
  1. Switch to the branch you want to merge into:

    css
    git checkout main
  2. Merge changes from the other branch:

    sql
    git merge feature-branch
  3. Resolve conflicts if necessary:

    If there are conflicts between the two branches, Git will prompt you to resolve them. You can use a text editor or a merge tool to resolve the conflicts.

  4. Commit the changes:

    Once the conflicts are resolved, commit the changes to the main branch.

Viewing commit history

To view the commit history of the project, use the git log command:

bash
git log

This will show you a list of all the commits in the repository, along with their commit messages, author names, and timestamps.

Undoing changes

If you need to undo changes that have been committed to the repository, you can use the git revert command. For example, to revert the last commit, run the following command:

git revert HEAD

This will create a new commit that undoes the changes made in the previous commit. If you want to completely remove the changes from the repository, you can use the git reset command with the --hard option:


git reset --hard HEAD~1

This will remove the last commit from the repository and reset the files to their state before the commit. However, be careful when using this command, as it will remove all changes made in the last commit and cannot be undone.

Checking the status of the repository

To check the status of the repository and see which files have been modified or added to the staging area, use the git status command:

git status

This will show you a list of all the modified files and their current status.

Pushing changes to a remote repository

To push your changes to a remote repository, first add the remote repository URL using the git remote command:

git remote add origin git@github.com:username/project.git

This will add a new remote called origin with the specified URL. Then, to push your changes to the main branch of the remote repository, use the git push command:

git push origin main

Pulling changes from a remote repository

To pull changes from a remote repository, use the git pull command:


git pull origin main

This will fetch the changes from the main branch of the remote repository and merge them into your current branch.

Cloning a remote repository

To clone a remote repository to your local machine, use the git clone command:


git clone git@github.com:username/project.git

This will create a new directory called project on your local machine and clone the remote repository into it.

fdf


Comments

Favourite post

Part 2 : TCS DCA python coding questions

TCS wings1 DCA python coding TCS elevate wings1 coding questions This post contains only coding questions asked in TCS digital wings1 or tcs elevate wings1 exam. If anyone want coding answer of these questions please comment me or reach me through email provided. Given a string str which consists of only 3 letters representing the color,(H) Hue, (S) Saturation, (L) Lightness, called HSL colors. The task is to count the occurrence of ordered triplet “H, S, L” in a given string and give this count as the output. This question was asked in April 21 Digital Capababilty Assessment Examples: A) Input HHSL Output 2 Explanation : There are two triplets of RGB in the given string: H at index O, S at index 2 and Lat index 3 forms one triplet of HSL. H at index 1, S at index 2 and Lat index 3 forms the second triplet of HSL. B) Input:   SHL Output: 0 Explanation : No triplets exists. In this 3 Palindrome, Given an input string word, split the string into exactly 3 palindromic substrings. Work...

Part 1 : TCS DCA python coding questions

TCS wings1 DCA python coding TCS elevate wings1 coding questions This post contains only coding questions asked in TCS digital wings1 or tcs elevate wings1 exam. If anyone want coding answer of these questions please comment me or reach me through email provided. Find how many Sexy Prime Numbers in a given range k and p.  Sexy prime means the prime numbers that differ from each other by 6. Where difference between two sexy prime numbers is 6. Constraint: 2 <=p<k<= 1000,000,000. Sample Input: 4  40 Output: 7 Explanation: [5,11] [7,13] [11,17] [13,19] [17,23]  [23,29] [31,37] Problem Description -: Given two non-negative integers n1 and n2, where n1 For example: Suppose n1=11 and n2=15. There is the number 11, which has repeated digits, but 12, 13, 14 and 15 have no repeated digits. So, the output is 4. Example1: Input: 11 — Vlaue of n1 15 — value of n2 Output: 4 Example 2: Input: 101 — value of n1 200 — value of n2 Output: 72 Consider the string S1 = 321 All char...

Calculator program using python tkinter

Project Name:- Simple Calculator Software using Python Tkinter. Dependencies:-                           1)Install  Tkinter- https://www.techinfected.net/2015/09/how-to-install-and-use-tkinter-in-ubuntu-debian-linux-mint.html                                 2)Install Math Library- for python3- pip3 install math  for python2- pip install math  ******************************************//************************************************************************ from tkinter import * from math import * class cal: def press( self ,n): self .text.insert(INSERT,n) def dl( self ): self .text.delete( 1.0 , 2.0 ) def equal( self ): self .exp= self .text.get( 1.0 ,END) try : self .result= eval ( self .exp) self .text.delete( 1.0 , 2.0 ) ...