Basics of Git

AI Perceptron
AI Perceptron
Published in
5 min readDec 19, 2020

Git is a free open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git is easy to learn and works faster. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, staging areas, and multiple workflows.

To, start working with GitHub go through the below instructions:

Search on google and click GIT and start installing

Start Installation

Click Next selecting all defaults until you get the below screen.

Create a new folder where you want to do the coding

Go inside the new folder created, click on the highlighted box. Type cmd and hit enter. It will open command prompt.

On command prompt type command “git init” This will initialize an empty git repository.

Add some code files at location. Type command “git status”. This will show status of all the files in the directory.

To add all the files in git at current location type command “git add .”. Check the status of files again after adding.

While trying to do the first commit you may get a message like below. Do the setups as described in the instructions. Do the commit again using the command “ git commit -m “Initial checkin” ”

Add some other code file or make changes in any of the existing code file and type git status.

Follow this steps if you dont have SSH

#on git bash

$ mkdir ~/.ssh

$ ls

$ ls -al ~/.ssh

Generating a new SSH Key

(Paste the text below, substituting in your GitHub email address.)

$ ssh-keygen -t ed25519 -C “abcdefgh@gmail.com

# start the ssh-agent in the background
$ eval $(ssh-agent -s)

#Adding a new SSH key to your GitHub account

$ clip < ~/.ssh/id_ed25519.pub (using upper arrow button)
(Copies the contents of the id_ed25519.pub file to your clipboard)

One time SSH connection needs to be setup to work with Remote. Follow the instructions on the below link.

#on cmd

>mkdir test

>dir

>cd test

>git@github.com:PYB01/Python.git

>dir

>cd python

>dir

>notepad test.py (notepad will be opened with the existing data in file for demo you can try to make some small changes ) or write the command “start notepad++ test.py”

>git status

>git add .

>git status

>git commit -m “changes done by yourname”

>git push origin main

Some Important commands:

git config --list :used to check and list all the configuration settings.git status -s:shows current state of working key or directory.git log:
git remote -v
a one-time operation:
git remote add origin gituser@git.server.com:project.git
git remote -v
a one-time operation:
git remote add origin gituser@git.server.com:project.git
git push origin main:pushes all of your data inside the remote repositorygit show <hash-code>
git diff
git pull --rebase
git stash
git branch fix01
git branch
git checkout -b fix01
git push origin fix01
git log fix01
git reset --hard origin/main
git diff main..b02
#for deleting a file from git
git rm <file>
git commit -m "Deleted the file from the git repository"
git push
git log --oneline # get all the hash for checkins
# use checkout with hash or HEAD^1 file_name

To clone a repository:

Create some local directory and run following command:

git clone git@github.com:PYB01/Python.git

Merge preserves history whereas rebase rewrites and simplifies it.

Github vs GitLab — GitLab for CI/CD and integration with kubernetes

Steps to be followed for development:

Locally Clone the remote repository where the changes has to be done or create a new repository. If the repository is already existing locally pull the changes from the main branch (source of truth)

git pull origin main #pull the latest changes on main
git checkout -b new_branch #Create and switch to a new branch locally
git status
git add .
git commit -m “checkin message”
git push origin new_branch #push the changes to new_branch
git checkout main #switch to main branch
git merge/rebase new_branch #merge/rebase main with new_branch
git push origin main #push to main
git branch -d new_branch
#delete the branch

While doing commit you might get below error. As there might be some recent remote code not present locally.

Run below commands

git pull origin main

git push origin main

Good code documentation on README.md

--

--