Git and GitHub

Piyush SatiPiyush Sati
7 min read

Before we dive into the world of Git commands and notes, I'd like to take a moment to extend a heartfelt thank you to Kunal Kushwaha. Kunal's tutorials have been an invaluable resource in my journey to mastering Git. His clear explanations and expert guidance have helped me navigate through the complexities of version control with confidence. Without his contributions to the coding community, this blog post wouldn't have been possible. Thank you, Kunal, for sharing your knowledge and empowering developers like me to grow and learn. #DSAwithKunal

Introduction

Git is a version maintaining tool, it saves the history of your project. But there are other tools/ui which help us see the history such as Github, Bitbucket, GitLab etc. *Who made What change and Where*

Linux Commands:

CommandFunction
ls -aLists a directory’s content
cdChanges the working directory
mkdirCreates a new directory
rmDeletes a file
touchCreates a new empty file
fileChecks a file’s type
sortReorders a file’s content
echoPrints a message as a standard output
diffCompares two files’ content and their differences
locateFinds files in a system’s database
sudoRuns a command as a superuser
suRuns programs in the current shell as another user
dfDisplays the system’s overall disk space usage
unamePrints information about your machine’s kernel, name, and hardware
hostnameShows your system’s hostname
jobsDisplays a shell’s running processes with their statuses
killTerminates a running process
shutdownTurns off or restarts the system
pingChecks the system’s network connectivity
wgetDownloads files from a URL
historyLists previously run commands
lfconfigDisplays the system’s network interfaces and their configurations
netstatShows the system’s network information, like routing and sockets
tracerouteTracks a packet’s hops to its destination

All the history of the project is saved in the git folder i.e. .git folder which is hidden.

Consider that you are in a wedding and need to click picture with the couple getting married. To maintain history we click couples.

-> guests go on stage whose photos not taken (untracked files; git commit)

-> click picture (push)

-> come out of stage ()

-> history saved

-> backstage (stash)

Git Commands:

Setup
Git CommandFunction
git config --global user.name “[firstname lastname]”set a name that is identifiable for credit when review version history
git config --global user.email “[valid-email]”set an email address that will be associated with each history marker
git config --global color.ui autoset automatic command line coloring for Git for easy reviewing
git config --list
git config --global --unset <variable>
Initialization
git initinitialize an existing directory as a Git repository
git clone [url]retrieve an entire repository from a hosted location via URL
git statusshow modified files in working directory, staged for your next commit
git add [file]
git add --alladd a file as it looks now to your next commit (stage)
git reset [file]unstage a file while retaining the changes in working directory
git diffdiff of what is changed but not staged
git diff --stageddiff of what is staged but not yet committed
git commit -m “[descriptive message]”commit your staged content as a new commit snapshot
git restore --staged <filename>remove the files from staging
Branch & Merge
git branchlist your branches. a * will appear next to the currently active branch
git branch [branch-name]create a new branch at the current commit
git checkoutswitch to another branch and check it out into your working directory
git merge [branch]merge the specified branch’s history into the current one
git logshow all commits in the current branch’s history
git remote add [alias] [url]add a git URL as an alias/name that you want to give(origin)
git fetch [alias]fetch down all the branches from that Git remote
git merge [alias]/[branch]merge a remote branch into your current branch to bring it up to date
git push [alias] [branch]
git push origin piyushTransmit local branch commits to the remote repository branch
git pullfetch and merge any commits from the tracking remote branch
git rebase [branch]apply any commits of current branch a head of specified one
git reset --hard [commit]clear staging area, rewrite working tree from specified commit
git stashSave modified and staged changes
git stash popwrite working from top of stash stack
git stash drop/cleardiscard the changes from top of stash stack
git stash listlist stack-order of stashed file changes
git show <commitid>show the commit id, who has commited

When we keep on commiting to our branch a sort of acyclic graph structure is formed.

Git commits have hashid i.e. they work as stack so if you remove one commit, all the commits after it will be removed (git reset)

What is upstream and adding it to local ?

origin url is my account but from where we fork a project that is called upstream url.

What is Pull Request ?

Lets say we have modified a file. You can create a new branch, checkout that branch, git commit and push. This will commit and push to that new branch, and main will be fine.

But Never do: git push upstream ❌; as it indicates you are trying to push to main and in original repo so always git push origin feature/branch

Pull Request or PR is basically a way you tell the owners of a repo that you have done some changes and now you are requesting them to pull your changes and merge into the stable/main branch.

For every new feature, create a new branch. 1 branch, 1 PR.

if for a branch a PR is already open, we cant create a new PR for it.

To remove a change, or pushed change ->

git reset <hashid>; git stash

This will remove the change from commit history but now you will have to force push it as online repo contains a commit which local doesnt i.e. git push origin <branch> -f

Upstream can easily merge and the changes will be reflected in main or branch chosen.

As you cant directly modify origin repo so why can they modify it. Therefore, we see that this branch is behind main by n commits. To solve this "Fetch Upstream"

git fetch --all --prune even the deleted ones will be fetched

git checkout main

get reset --hard upstream main

git push origin main

or

git pull upstream main (internally will do same as the above 2 commands)

git push origin main

Squashing Commits

branch is always created from head so make sure head is up to date.

When we have multiple commits, and we want to merge all together so just, reset it so all commits are on unstage then commit again and push,

git rebase -i <hashid> commit above can be squashed or pick

Esc : X

Squashed into one commit into the one which is picked; pick one stays a separate commit whereas squashed are merged to the bottom commit of stack.

Merge Conflicts

When more than one change in same line, same file from different commits/persons. Git will ask your help. Which you will have to do manually.

0
Subscribe to my newsletter

Read articles from Piyush Sati directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Piyush Sati
Piyush Sati

Looking for a new career venture to upskill myself and work in the IT field. System Engineer @Infosys. Gold Badge(5 star) @ HackerRank. I am skilled in Java, Spring, SpringBoot Maven, Hibernate, MicroService Architecture. Actively learning MERN Stack Development. Hardworking, focused and punctual individual with good analytical and problem solving skill. I have the ability to learn new things quickly and to juggle and prioritize different tasks. Proficient in technical communication. Trained in - HTML, CSS, JS, ReactJS, Bootstrap, Java, Core Java, Spring, SpringBoot, Maven, Hibernate, REST API, Microservice Architecture May not a 'know-it-all' but definitely a 'learn-it-all' guy. Always on the lookout for challenging opportunities.