Skip to content

haider-patanwala/About-Git

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Git Cheatsheet

If you have any doubt/question then please feel free to raise the issue on github or can contact me on my other social media profiles and link of those social media profiles can be found on my github profile page. And also if there's any correction or new addition from your side for my cheatsheet then I would be priviliged to get your contritbution. 🙏🏻 Hope you have a good time here 🥰

All the following steps are prefered to be followed and executed on 'Git Bash' after installing it on your machine.

To check your git version

git --version
  • Three cases:

If your Git version is 2.14.1 or earlier:

Uninstall Git, download the latest Git, and install it again.

And versions between 2.14.2 and 2.16.1:

Use command

git update

If the version is equal to or greater than Git 2.16.1(2):

Use command

git update-git-for-windows

1st time git setup

STEP 0.1

git config --global user.name yourname

STEP 0.2

git config --global user.email "[email protected]"

To know it is set up or not :

git config --global user.name

(your username as output)

git config --global user.email

(your email as output)

STEP 1

(This step is optional is you already have an existing .gitignore file) "touch" creates a new blank file of .gitignore

touch .gitignore

edit the .gitignore file and add the name of the folder which we dont want to upload

STEP 2 - Initialization

FYI : If we want to have a repository then 2 options we have,

1. git init (it initializes from the start)
OR
2. clone (it clones a repo we want and clones it's whole data from server)

Will create a .git file, keep hidden files enable to see it

git init

STEP 3 - Staging

Very imp Flow of git -

    Untracked file(On local device)----> Staged(filewhich we wish to put on git) <---|
					    |					     |
					    |	{ Commit }			     |
					    v					     |
				Unmodified file on git				     |
		(Goes on git which we can edit/remove)[snapshot taken by git]	     |
					    |					     |
					    |  (If edited)			     |
					    v					     |
			          Modified file on git >------(Back to staging)------|

To put all the contents at staging area and . means staging all new and modified items excluding deleted ones.

git add .  

If dont want to put all files then use the following syntax

git add filename

If want to put all the files of the directory at staging then,

git add -A

If want to put only the modified and deleted items at staging then,

git add -u

To stage a New_folder(*Remember, don't keep a space in the folder name and keep as eg. New_folder) :

git add New_folder/

To stage say a specific.txt file in a nested folder :

git add folder1/folder2/folder3/specific.txt

👉🏻 Can check status ✔ now by

git status

STEP 4 - Commit

  • git commit means taking a snapshot

  • So git add and git commit makes a snapshot associated to the changes we made

  • But right now nothing is added to my repo so using message for commit as "Initial commit"

If this command is executed then an editor will open then press i to edit and write the message Initial commit and then press Esc key and then type :wq to save and exit the file.

git commit

  • ALTERNATE EASY METHOD
git commit -m "Initial commit"
  • I prefer the following commands as it skips staging and directly commits :
    • -a Commits modified and deleted files
    • Remember this following will mostly work when we have done tracking(staging) with the git add command atleast once for tha repository.
git commit -a -m "msg"

Or

git commit -am "msg"

  • If want to commit one one files separately with seaparate messages then,
git add file1.txt
git commit -m "Commiting file1.txt with custom message"

Similarly,

git add file2.txt
git commit -m "Commiting file2.txt with custom message"
  • To know if commit is done, check the log which will give information about about all commits.
git log --oneline

Commit done 🎉 , now Push remaining

To finally push the "master" branch on "origin" url

git push -u origin master
  • If once push -u origin is used then next time we just write git push which will by default push it the branch which was last commanded with -u
  • Technically, the -u flag adds a tracking reference to the upstream server you are pushing to.

What is important here is that this lets you do a git pull without supplying any more arguments.

For example, once you do a git push -u origin master, you can later call git pull and git will know that you actually meant git pull origin master.

Otherwise, you'd have to type in the whole command.

Getting Started with ➡ Github ⬅

Make a repo on github with no files(no readme, etc) just as an example 😉

TO CONNECT TO GITHUB'S REMOTE REPOSITORY WITH OUR LOCAL REPOSITORY(Folder on the PC which we need to upload on github)

git remote add origin [email protected]:haider-patanwala/About-Git.git

[email protected]:haider-patanwala/About-Git.git is the SSH link of the github repository on which we want to connect and upload our local repository. MEANING A REMOTE NAMED origin IS ADDED AND OUR URL CAN BE CALLED AS origin

  • TO SEE REMOTE
git remote

Output:

origin
  • TO SEE URL
git remote -v

Output :

origin  [email protected]:haider-patanwala/About-Git.git (fetch)
origin  [email protected]:haider-patanwala/About-Git.git (push)

⚠⚠ NOW GITHUB IS NOT LINKED WITH OUR PC SO TO PUSH THE LOCAL REPO TO GITHUB DO THE FOLLOWING :

  1. Go to this Github documentation by clicking here to generate new ssh key.

(Open Git Bash if you're using windows then follow then enter these commands)

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

    ssh-keygen -t ed25519 -C "[email protected]"
  2. To generate Agent pid

    eval "$(ssh-agent -s)"

    and add the key

    ssh-add ~/.ssh/id_ed25519

If things are not working out check this

  1. Use this command To read the ssh key or go to the directory given by previous eval command and opened .pub file with notepad

    clip < ~/.ssh/id_ed25519.pub
  2. Copied the ssh key and added it as a new ssh key on Github's Acc settings > SSH Keys > New SSH Key(green button).

  3. Went to repo's "Code" tab and clicked on "Code 🔽" green button and copied ssh url.

  4. Copied the SSH url which looks like [email protected]:haider-patanwala/About-Git.git

  5. Have to set the Github's SSH url to the local machine

git remote set-url origin [email protected]:haider-patanwala/About-Git.git
  1. To finally push the "master" branch on "origin" url
git push -u origin master

(If once push -u origin is used then next time we just write git push which will by default push it the branch which was last commanded with -u)

  • To push to another branches on GitHub ,
  1. git checkout newBranch
  2. git push -u origin 

Miscellaneous

1) Directly commit by skipping staging area

git commit -a -m "Skipped staging area and commited directly"

-a is selecting All for stagging and -m is for Message.

2) Remove the file

From local storage & staging area

git rm `filename`

From staging area only, local one will remain the same

git rm --cached `filename`

  • Delete file/folder from remote repository :

1] First deleting from local & staging area.

git rm --cached `filename`

For a folder (*Remember, don't keep a space in the folder name and keep as eg. New_folder),

git rm --cached -r New_folder/

2] Then just commit nd push the changes.

git commit -m "Removed"
git push

  • Delete multiple files in one go 😎

1] From both local and remote repository

git rm file1.png file2.txt file3.py 

Or

1] From only remote repository while keeping the local one untouched.

git rm --cached file1.png file2.txt file3.py 

2] Then just commit nd push the changes.

git commit -m "Removed"
git push

3) Shorthand for status

$ git status -s
MM  about.html(this will be in green meaning modified in staging area)(2nd M will be in red meaning modified in working tree)
M site.html (this will be in red meaning modified in working tree )
?? waste.html

4) Clone Command

  1. Making a new folder on PC and opening git bash(by right click menu) over there to clone into that folder.

  2. Will go to any public repository on Github and under code tab will copy the https link.

  3. In the new folder's git bash we'll write

    git clone https://github.com/haider-patanwala/To-Do-List.git haider

    where https.... is the link of repo to be cloned and haider is the folder name where the repo to be cloned inside our opened folder.

    git clone https://github.com/haider-patanwala/To-Do-List.git

    *If "haider" folder name not given then a folder of remote repository's name will be created.

  4. Clone to a specific_branch

    git clone -b specific_branch remote_link_SSH_or_HTTPS

5) Branch

  • A branch is used to make a different workspace on the repository to dump/upload on that different branch to work separately without causing any trouble to the anothe branches.

  • An analogy of a Tree is perfectt example to understand as different branches are separated on the same Tree where Tree is considered to be the repository.

  • BY DEFAULT, BRANCH IS "MASTER".

TO CREATE A BRANCH

git branch `name_of_branch`

TO KNOW THE NAMES OF ALL BRANCHES:

git branch

SHORTHAND FOR CREATING AND ENTERING(SWITCHING TO IT) THE BRANCH AT THE SAME TIME

git checkout -b newBranch

TO SWITCH TO ANOTHER BRANCH

haider@LAPTOP ~/OneDrive/Desktop/New folder (master)
$ git checkout feature1

Switched to branch 'feature1'

haider@LAPTOP ~/OneDrive/Desktop/New folder (feature1)
$ git status
On branch feature1
nothing to commit, working tree clean

**Keep the file open and see the changes when switching between branches.

6) Playing with branches

  • To push to a specific branch to a remote names origin
git push origin branch_name 
  • To merge a specific branch with another branch say master branch then switch (checkout) to it and do the following
git merge specific_branch
  • Delete a specific branch from the local repository
git branch -d specific_branch
  • Delete a specific branch from the remote origin
git push origin --delete specific_branch
  • To rename any_branch
git branch -m new_name

Then after all these or any of these push changes forcibly,

git push -f origin myBranch

7) Logs

TO GET LOG OF ALL OUR COMMITS

git log

TO FILTER OUT LOGS OF COMMITS.

FOR EG. TO GET LAST 2 COMMITS (it also shows the changes)

git log -p -2

(press q to exit from the log viewer)

8)Compare changes

WILL COMPARE AND SHOW THE DIFFERENCES BETWEEN THE CURRENT FILES(WORKING TREE) AND STAGED FILES

git diff

WILL COMPARE AND SHOW THE DIFFERENCES BETWEEN THE LAST COMMIT AND STAGED FILES

git diff --staged

9) .gitignore

.gitignore file has the list of all the files and folders which we dont want to track with git. SHOULD BE KNOWN :

  1. TO IGNORE ALL THE FILE-TYPES(.log), ADD THIS IS IN .gitignore eg: (.log)

    *.log
  2. TO IGNORE A FOLDER USE A FORWAD SLASH

    `folder_name`/

10) Restore files

To restore files from last commit. Used to get back the last version of the file if someone has wrongly edited and saved it. Essentially matching with the last commit !!

git checkout `filename`

To match all the files with last commit and change and them according to the last commit !!

git checkout -f

11) Get the remote repository to local PC

Can make a new folder and run git init by opening git bash in that folder.

git clone https_link

Or

git pull https_link

12) Removing Staged files from Staging level

git restore --staged staged_file_name_or_path
git reset staged_file_name_or_path
git reset HEAD staged_file_name_or_path

HEAD points to location at which our repository is at current state at current time & space.

  • To remove all staged files from staging area
git restore --staged 
or 
git restore --staged .
git reset 
git reset HEAD 
or
git reset HEAD .
or
git reset HEAD --

13) To undo changes in the file at local level :

It's basically Ctrl+Z IMP : But it's different because the file is not yet commited/staged.

git revert file.txt

Or

git checkout -- file.txt

14) Getting back to previous commit

i) To go back to previous commit

revert HEAD reverts the current state(HEAD) to the last commit.

git revert HEAD

ii) To go back to a specific commmit

12abc34 is the 7 digit example git commit ID. To know the git commit IDs of all the commits made.

git log --oneline

Then use that ID for revert command.

git revert 12abc34

After this command your terminal's text editor(Most likely Vim for git bash) will open and don't do anything if you dont want to write a custom commit message. Just do :wq to save and exit the text editor which will make the revert work.

If you want to write your custom git message then press i and start editing the first line on the editor and then exit the edit mode by pressing Esc key and then :wq.

  • Another way,

To know the git commit IDs(40 characters long) of all the commits made.

[Press q to exit the editor viewer after git log command.]

git log 

Then use that 40 characters long ID for revert command.

git revert 4e28d49dc37b66v755b6f95dc70c8e579z3830f8

The above ID is example and please dont try to copy paste the same ID

Then,

git push

15) git reset - To destroy previous commits

Say there are 5 commits, 1 being oldest(first) and 5 being latest. Now you want to go to 2nd commit and delete all 3rd,4th,5th commits. I mentioned DELETE i.e remove it's history totally and not like git revert command.

git reset is very powerfull to destroy those commits. 2 types :-

1) git reset --hard

--hard will remove the commits history and undo changes of the commits at both local and remote levels. 40 digits commit ID mentioned in the command is where we want to shift our HEAD(Pointer) to and delete every commit above it.

git reset --hard 4e28d49dc37b66v755b6f95dc70c8e579z3830f8

The above ID is example and please dont try to copy paste the same ID.

Then use -f flag which means push forcibly.

git push -f origin myBranch

Need to force push like this because we came back in the commits history on local level using git reset

But the remote is ahead of local in commits as the changes aren't yet pushed.

Thereforce, will need to force the push using -f.

2) git reset --soft

--soft will destroy commits at remote level as well as local level BUT won't undo/revert the changes of the destroyed commits at local repository. Therefore, called as soft reset.

git reset --soft HEAD~x

~ = Tilde sign x = number how much back in commit history you want to go. Example : HEAD~3 means go back three commmits from the HEAD. Then, we'll offcourse have to push the changes which will delete the commits even from the remote and won't display them.

  • 2 ways to push forcibly, git push or git push -u origin wont work as we want to force push🌝
git push origin +master

Or

git push origin +master --force

⚠ However, a huge downside of these reset commands(both hard and soft) is that it just makes the commmit invisible from our github repository

But when we have the exact link to the destroyed commit then we can access it's full files like try this

⚠Also, if there is only one commit in the repository then that cannot be deleted by these commands as these command take the Head to the mentioned commit by deleting the ones above it and not destroy the mentioned commit.

16) Rename message of last commit

git commit --amend

After this command your terminal's text editor(Most likely Vim for git bash) will open.

To write your message press i and start editing the first line on the editor and then exit the edit mode by pressing Esc key and then :wq.

Important lesson I learnt

I wanted to unstage all the staged files so used the command git reset HEAD

But unknowingly I deleted dozens of important files from my local repository.

So the followings steps to take in such situation to restore the file of the local repository.

  • Git keeps a log of all ref updates (e.g., checkout, reset, commit, merge). You can view it by typing:
git reflog

Somewhere in this list is the commit that you lost.

Let's say you just typed git reset HEAD and want to undo it. My reflog looks like this:

Screenshot

The first line says that HEAD 0 positions ago (in other words, the current position) is 8efd6f0. It was obtained by resetting to HEAD~. The second line says that HEAD 1 position ago (in other words, the state before the reset) is f7e9df2. It was obtained by checking out a particular commit (though that's not important right now).

So, to undo the reset and go to a specific commit (85bfa92- where I want to go back, from where I lost the data)

git reset 'HEAD@{4}'

or

git reset 85bfa92

This will give lost of all files which were lost.

To obtain then back, we'll use the command checkout

git checkout 22_Class_Based_Components.md 22_news_app/.gitignore 22_news_app/README.md 22_news_app/SampleResponse.json 22_news_app/package-lock.json 22_news_app/package.json 22_news_app/public/favicon.ico 22_news_app/public/index.html 22_news_app/public/logo192.png 22_news_app/public/logo512.png 22_news_app/public/manifest.json 22_news_app/public/robots.txt 22_news_app/src/App.css 22_news_app/src/App.test.js 22_news_app/src/components/loading.gif 22_news_app/src/index.css 22_news_app/src/index.js 22_news_app/src/logo.svg 22_news_app/src/reportWebVitals.js 22_news_app/src/setupTests.js 23_Components_structuring.md 24_News_API.md 25_rcc_States.md 26_Looping_array.md 27_Fetching_from_API.md 28_Next_Previous_pages.md 29_Loading_Spinner&variable_pageSize.md 30_Categories&propTypes.md

Writing the names of all files together to restore them in one go.

17) How to insert image in markdown ?

![alt text for screen readers](/path/to/image.png "Text to show on mouseover")

18) How to color text in markdown ?

<span style="color:yellow">some *yellow* text</span>

19) Stash command

  • Used when we want to save current working directory/branch's work which can be incomplete and proceed to some other branch's work
  • Useful while switching branches
  • Useful when dont wanna commit incomplete progress of current branch.
  • Normally we commit and then switch branches
  • But if switching without doing a commit then 2 of the following things can either happn
    1. Switch to other branch carrying changes to other changes
    2. Git wont allow to switch and will ask to commit/stash the changes
  • The command allows switching branches without commiting to the current branch
  • Stash meaning is "to store something safely ata hidden place".
  • So git temporarily saves the data without committing
  • Simple easiest way to stash current work in the current branch is
git stash
  • Giving name to the stash
git stash save cool_name
  • Listing the available stashes
git stash list
  • To get a specific stash back
git stash apply `index`
  • TO get back the stashed changes
git stash pop

Simple Walkthrough for Pushing

Do the following when understood all of the above steps

Applicable when want to push a local project to a new github repository

git init
git remote add origin [email protected]:SSH_LINK.git
git remote
git status

Status will show that no commits and untracted files are there excluding the ones which are listed in .gitignore

git add -A
git commit -m "msg"
git push -u origin master

Related

Here is a related article which is really good and can be used as Git cheat-sheat too.

javatpoint

About

Cheat Sheet and Guide for learning Git

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published