Git and GitHub – How to start with Git (Part -3)

Posted by

1. Create a Repository (repo)

A repository is a storage location where your project files and their version history are kept.

Steps:

  • Open Git Bash.
  • Navigate to the directory where you want to create the repository.
  • Run the following command to initialize a new Git repository:
git init

Example:

mkdir my_project
cd my_project
git init

This creates a new directory called my_project, navigates into it, and initializes an empty Git repository.

.git file is hidden file to see it , enable the hidden item from view section

2. Write Your Code

Create or add the files for your project. You can use any text editor or IDE to write your code.

Example:

  • Create a new file named main.py and write some Python code in it.
# main.py
print("Hello, Git!")

or

3. Add a File to Git

To start tracking a file with Git, you need to add it to the staging area using the git add command.

Steps:

  • Use the following command to add a file to the staging area:
git add <filename>

Example:

git add main.py

This command stages the main.py file, telling Git to include it in the next commit.

4. Commit a File to Git

A commit saves the current state of the files in the staging area to the repository. Each commit has a unique ID and an associated message describing the changes.

Steps:

  • Use the following command to commit the staged files:
git commit -m "Your commit message"

Example:

git commit -m "Add main.py with a simple hello world script"

his command commits the main.py file with the message “Add main.py with a simple hello world script”.

Check Git config list

$ git config --list
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=C:/Program Files/Git/mingw64/etc/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
pull.rebase=false
credential.helper=manager
credential.https://dev.azure.com.usehttppath=true
init.defaultbranch=master
user.email=jami.cotocus@gmail.com
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
user.name=Jami Raj
user.email=jami.cotocus@gmail.com

Summary of the Steps with Full Example

Create a repository:

    mkdir my_project
    cd my_project
    git init
    

    Write your code:

    • Create a file main.py and add the following code
    print("Hello, Git!")
    

    Add a file to Git:

    git add main.py
    

    Commit a file to Git:

    git commit -m "Add main.py with a simple hello world script"
    

    Command History

    git init
    touch file2.java
    touch file3.java
    git add file1.java
    git config user.name "Jami Raj"
    git config user.email "jami.cotocus@gmail.com"
    git config --list
    git commit -m"Adding first commit"
    
    
    guest
    0 Comments
    Inline Feedbacks
    View all comments
    0
    Would love your thoughts, please comment.x
    ()
    x