GitCommandsPro Your Guide to Essential Git Commands in 2024

Table of Content

1

Check git version

2

Configuration Settings

3

Initializing a repository

4

Staging files

5

Viewing the status

6

Committing the staged files

7

Skipping the staging area

8.

Removing files

9

Renaming or moving files

10

Viewing the staged/unstaged changes

11

Viewing the history

12

Viewing a commit

13

Unstaging files (undoing git add)

14

Discarding local changes

15

Restoring an earlier version of a file




    1.     Check git version

git --version

2.     Configuration Settings

Levels of defining this settings are

System level -  apply to All Users of computer.

Global – all repositories of the current user.

Local – the current repository. Or a repository in the current folder.

        Name 

git config --global user.name "YOUR_NAME"

        Email

git config --global user.email YOUR_MAIL@gmail.com

        Default Editor

(for setting vscode)

git config --global core.editor "code --wait"

         To edit all the global settings

(will open the default editor to edit the global settings)

git config --global -e

        Line Ending

(For windows)

config --global core.autocrlf true

(For MAC)

git config --global core.autocrlf input

         To disabling fast-forward merging for particular repository.

git config ff no    	        	

This will disable fast-forward for current repos.

git config --global ff no	

This will disable fast-forward for all repos. Now we can use “git merge branch-name” to merge a given branch with the main branch without fast-forwarding merge. 

    3. Initializing a repository

git init

    4. Staging files

What is the staging area(also known as Index)?

To save changes in the git repository we take a snapshot(take a photo) of the folder, but before taking this snapshot all the changes are mentioned in an area called staging area. Here we can remove (or unstage) files if we don’t want to add them in the repository.

git add file1.js                     	

            Stages a single file.

git add file1.js file2.js                	

Stages multiple files.

git add *.js                         	

Stages with a pattern.

git add .                     	

Stages the current directory and all its content. 

    5.Viewing the status

git status                                          	 

Full status.

git status -s                                      	

Short status.

    6.Committing the staged files

    This will take a snapshot of the files available in the staging area.       

git status -s   

Commits with a one-line message. Use Past Tense for verb.

git commit 

Opens the default editor to type a long message. 

    7.Skipping the staging area

git commit -am “Message”     	

flag “a” means for all modified files, “m” for message.

    8.Removing files

git rm file1.js                                 	

Removes from working directory and staging area.

git rm --cached -r file1.js           	

-r” Removes entire file.

- - cached”  Removes from staging area only.                     

    9.Renaming or moving files

git mv file1.js file1.txt               	

rename the file name file1.js => file1.txt.

mv” move command.

    10. Viewing the staged/unstaged changes

git diff                                               	

Shows unstaged changes. Means show difference between content of file in directory & content of file in staging area.

git diff --staged                             	

Shows staged changes . Means show difference between content of file in staging-area & content of file in repository.

git diff --cached                            	

Same as the above.

to get out of the diff command press “Q”.

    11.Viewing the history

git log                                               	

Full history of commits.

git log --oneline                          	

Full history of commits with short description.

git log --oneline --all --graph   	

Full history of commits with short description with all the  diversity in branches.

git log --reverse                            	

Lists the commits from the oldest to the newest.

    12.Viewing a commit

git show 921a2ff                          	

Shows the given commit.

git show HEAD                              	

Shows the last commit.

git show HEAD~2   	

Two steps before the last commit.

git show HEAD:file.js   

Shows the version of file.js stored in the last commit.

    13.Unstaging files (undoing git add)

git restore --staged file.js    	

            Copies the last version of file.js from repository to index(staging-area).

            restore” just copy the content of file from next stage of mentioned flag and paste it to the file               which is in mentioned flag, here flag used is “staged” so the content of file “file.js” is copied from             repository and pasted to “file.js” in staged-area. 

    14. Discarding local changes

git restore file.js                           	

Copies file.js from index to working directory.

git restore file1.js file2.js      	

Restores multiple files in working directory from index.

git restore .                                     	

Discards all local changes (except untracked files as it is not there in staging-area).

git clean -fd                                    	

Removes all untracked files.

    15.Restoring an earlier version of a file

git restore --source=HEAD~2 file.js

We are restoring the file “file.js” to the previous version.

Here HEAD~2 can be replaced by “id” given by git to a particular version.



Comments

Popular posts from this blog

Formatting MySQL Dates

SOAP Explained: With Javascript

Mastering MySQL Timestamps