Skip to content

Harshverm776/git-commands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Git Commands

1. Install git

check version git --version

2. Configuring some settings

Like Name, Email, Default Editor & Line Ending

Different levels for setting - System, Global & Local.

git config --global user.name "Harsh Verma"
git config --global user.email harshverm776@gmail.com
git config --global core.editor "code --wait"
git config --global -e

Line endings -

Windows macOS/Linux
\r\n \n
  • For Windows
git config --global core.autocrlf true 
  • For Linux
git config --global core.autocrlf input

3. Taking help

Use -h or --help

4. Initializing a Repo

git init

Additional cmds -

  • Make directory - mkdir Moon

  • Open directory - cd Moon

  • Open a file -

    • start .git for Windows
    • open .git for Linux
  • Remove a file -

    • rm -rf .git
  • Listing files -

    • ls
    • ls -a

5. Staging Area

For checking the state of repo and staging area

git status 

Additional cmds -

  • Creating a new file echo hello > file1.txt
  • Appending content echo world >> file1.txt
  • Adding files to staging area -
    • git add file1.txt file2.txt
    • git add *.txt
    • git add .

6. Commiting Changes

  • Adding short comment
git commit -m "Initial commit"
  • Adding short and long comment
git commit

7. Skipping the stagging area

Rarely used

git commit -a -m "Some message"

or

git commit -am "Some message"

8. Removing files

WD : Working Directory
SA : Staging Area

Removing file from both WD & SA

git rm file2.txt 

Additional cmds -

  • File only remove from WD - rm file2.txt
  • Checking SA - git ls-files

9. Renaming or Moving files

Changes reflected in both WD and SA

git mv file1.txt main.js 

Additional cmds -

  • Renaming file - mv file1.txt main.js

  • Adding to SA - git add file1.txt main.js

10. Ignoring Files

We can add file names like logs/ *.class *.log bin/

echo logs/ > .gitignore

or

echo > .gitignore

Removing a file only from SA -

git rm --cached bin/
git rm --cached -r bin/

11. Short status

git status -s

12. Viewing the staged and unstaged changes

  • Staged changes
git diff --staged 
  • Unstaged changes
git diff

Additional cmds -

  • Open file in cmd - cat file2.txt
  • Open file in cmd with numbers - cat -n file2.txt

13. Using Visual diff Tools

  • Configuring VScode-
git config --global diff.tool vscode
  • Configuring launching -
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
  • Viewing unstaged changes -
git difftool
  • Viewing staged changes -
git difftool --staged

14. Viewing the history

  • Full logs -
git log
  • Logs in single line -
git log --oneline
  • Reverse Order -
git log --oneline --reverse

"space" for moving a head and "q" for quitting

15. Viewing the commit

  • Using unique identifier -
git show 921a
  • Viewing Last commit -
git show HEAD 
  • One step back -
git show HEAD~1
  • Viewing particular file -
git show HEAD~1:.gitignore
  • Listing all the files -
git ls-tree HEAD~1

16. Unstaging the files

git restore --staged file1.txt

17. Discharding local changes

git restore .

Additional cmds -

  • Removing all untracked files -
git clean
git clean -fd

18. Restoring a file to an Earlier Version

git restore --source=HEAD~1 file1.js


Sources :

About

Contain basic commands of git.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published