Skip to content

All you need to know to start with git! Keeping commiting ;) #iykwim

Notifications You must be signed in to change notification settings

code-lucidal58/git_inception

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 

Repository files navigation

GIT BASICS

It is tough to work without Git in this world of uncertainties. You never know when you desktop goes down and BOOM you lost all your projects. It is recommended for every developer to be committed to git ;)

Git is a software that allows you to keep track of changes made to a project over time. Git works by recording the changes you make to a project, storing those changes, then allowing you to reference them as needed.

Few key concepts

Git is a distributed and decentralised source control system. Most operations are local. Internet is required when changes are to be pushed to a remote repository. Each commit is identified with SHA1 hash. HEAD is pointer to last commit in current branch. Remote is a related repository that is not local.

Going to some depth

A git project has three main parts:

  1. Working directory: This is the place where the user make changes to the file in the project. A folder is converted to a git repository using git init. To clone(bring to local system) a git repository git clone <repo_url> is executed.
  2. Staging area: This is the place where all the changes the project are listed. Local changes are moved to staging area using git add <file/folder names>.
  3. Git Repository: all latest files/folders are saved as a different version in the project.This is done using git commit -m<message for the commit>. Adding message to the commit is mandatory.

NOTE: As shortcut to add and commit all changes in tracked files in working directory is

git commit -am \"<message>\"

Local changes to Remote repository: git push
Remote repository changes to Local: git pull

In git workflow, changes are made in the working directory, files are added to the staging area and saved in the repository.

Important git commands

Get Help

git help

This shows the different options available in git. git help <command> will show the documentation of the specified command only.

Configuring Git
Git must have the owner's name and email ID. This is preferably the user name and email ID that is linked to online VCS like Github or BitBucket.

git config --global user.name "<username>"
git config --global user.email "<email address>"

These details can be viewed using git config --global --list. The configuration settings are saved in the working directory in a file name .gitconfig.

Creating a folder along with git initialisation

git init <project-folder-name>

A .git folder is created whenever git is initialised.

Status of changed files

git status

Untracked files mean the files has some changes but git has not started tracking them.

Add a file to staging area

git add filename #OR
git add * #OR
git add . #OR
git add filename_1 filename_2 #OR
git add -A

Here, each command has their own importance and may behave different in different situations.

See the changes made to a staged file

git diff filename

View all commits
Git stores history of commits of a branch in chronologically order in repository. This is helpful when a reference to previous versions of project is required.

git log

This has SHA1 key, author, date and message for each commit. Flags can be added to have a more concise view of the commits. Try this:

git log --oneline --graph --decorate --color

Move files to different directory inside repository
Here index.html is moved from the root directory to a directory named web inside root.

git mv index.html web

Gitignore

Git provides user an option to exclude files or filename following a pattern from being added to staging area. This is done by creating a file named .gitignore. One pattern per line. Here is a sample .gitignore file.

__pycache__/*
.idea/*
*/__pycache__/*

Backtracking

As mentioned earlier, HEAD is the current commit. To look for changes in it:

git show HEAD

The output is same as git log plus changes made to file.

Removing file from staging area

git rm --cached filename

Git will stop tracking this file, as well.

Removing file from current commit

git reset HEAD filename

Git will keep tracking this file but removes it from latest commit.

Revert back to the content before the latest commit

git checkout HEAD filename

There is a shortcut to this command:

git checkout -- filename

The first 7 characters of the sha of last commit can also be used to unstag changes.

git reset 96g12673

Git Branching

The default branch that we work is master. Git provides option to create branches to experiment with versions of the project. The main project will be MASTER branch. The other branches need to merged to MASTER to update the main repository. New branch is a different version of the Git project. It will have all commits from MASTER. To check the current branch:

git branch

The output will be the list of all branches with asterisk(*) mark on the current branch. To create a new branch:

git branch new_branch

To switch to another branch:

git checkout branch_name

After switching, staging and commiting is done in the same way as before. To merge the changes with MASTER branch, first switch to master branch using git checkout master, then:

git merge branch_name

There will be merge conflicts if the same file is edited in master as well as any other. Many branches can be created in a Git project. But, the end requirements reflecting all changes into the master branch. After merging the branches can be deleted:

git branch -d branch_name

If a branch is not merged to master, then to delete that branch, run:

git branch -D branch_name

Generating SSH keys

  • Open command prompt and navigate to root user directory. E.g. /User/aanisha/
  • Move into .ssh folder. If it does not exist, create one : mkdir .ssh.
  • ssh-keygen -t rsa -C "aanisha.mishra05@gmail.com" to create a new SSH key.
  • It will ask for a file name to save the details and a password to protect. Give the details as per requirement. Now, two file are created based on the name given by user. One of them has .pub extension. This file has the content that can be copied into on of the VCS cloud clients like Github. The key generated will be unique to your laptop.

Now, come back to the same cmd session, and run ssh -T git@github.com(provided you have added the key to your GitHub profile). Your laptop and GitHub and now in mutual connection. This process is recommended for remote works.

Working with collaborates

A remote is a shared Git repository that allows multiple collaborators to work on the same Git project from different locations. Collaborators work on the project independently, and merge changes together when they are ready to do so.

To contribute to a remote project, the project first needs to be cloned to local. This is done as:

git clone remote_location clone_name

repo_location is address of the place where the repository is. It can be a file path or URL. clone_name is the name of the directory where Git project is cloned. By default, git names the remote connection as origin. All remotes can be views using:

git remote -v

Push an existing directory to remote

  • Create a repository in remote.
  • Copy the SSH link of the repository
  • Open command prompt inside the local directory and run git remote add origin <the link copied>.
  • Git pull to bring all remote changes to local.

Git Remote Command while collaborating

To reflect changes in git to you system:

git fetch

This will not update the local repository but will bring the changes to remote branch. To integrate origin (master) into local repository, git mergin origin/master is used.

To push changes to the origin:

git push origin branch_name

After pushing the changes to a branch, they can then be merged with master.

Sources: Udemy, Codecademy

About

All you need to know to start with git! Keeping commiting ;) #iykwim

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published