My Git Cheat Sheet

Nihal Dias
2 min readDec 12, 2020

--

git pull :Brings all online changes to Working Directory
git status : Shows if Working directory is up-to date with online and vice versa(if not shows changed filenames)
git diff : Shows parts where changes were made in all files in Working directory
git checkout: Undo all changes in Working Directory to match online repo(master/remote repo)
git checkout file-or-folder-path : Undo all changes for file or folder in Working Directory compared to online repo
git add filename : adds file from working directory to staging area to be committed and then pushed
git add . : adds everything in current folder
git commit -m “fix:bug” : Commits files from the staging area to the local repo to be pushed
git fetch: used to get files from the remote repository to the local repository
but not into the working directory.
git merge: used to get the files from the local repository into the working directory.
git push : pushes the changes to the online repo(master/remote repo)
git push = git fetch + git merge

In order to delete a local GitHub repository, use the “rm -rf” on the “.git” file
located at the root of your Git repository.

$ rm -rf /.git

For Git cloning your own repo
git clone repo_link
echo “# Some Text describing the repo” >> README.md(destination file name)
git add README.md
git commit -m “first commit”
git push -u origin main(or even just git push works when there are no branches)

For making a PR(Pull request) to a different repo
1.Find a project you want to contribute to
2.Fork it
3.Clone it to your local system
git clone forked_repo_link
4.Make a new branch

Create a new branch by issuing the command:

git checkout -b new_branch_name

Create a new remote for the upstream repo with the command:

git remote add upstream forked_repo_link

5.Make your changes
6.Push it back to your repo(add, commit,push)

git push -set-upstream origin new_branch_name

7.Go back to forked repo on GitHub and click the Compare & pull request button
8.Click Create pull request to open a new pull request

( From GitHub-> You get something like this usually when creating a new empty repo on GitHub)
…or create a new repository on the command line(Use this to make initial modifications to your new GitHub repo)

mkdir <name_of_repo>
echo “# Repo_Name” >> README.md
git init
git add README.md
git commit -m “first commit”
git branch -M main
git remote add origin repo_link
git push -u origin main

…or push an existing repository from the command line(Use this to push files that were locally ready to be pushed to your new GitHub repo)

git remote add origin repo_link
git branch -M main
git push -u origin main

--

--

Nihal Dias
Nihal Dias

Written by Nihal Dias

Just your run-of-the-mill Software Developer who's also an anime fanatic. I write about Software Development, Cloud Computing and Machine Learning.

No responses yet