GIT - Every SWE's friend

Git provides Version Control + Distributed Collaborative working

Check version

git --version

Configuring Git

Git is configured for different levels

System : All users

Global : All repositories of the current user

Local : Current repository

git config --h
git config --global "Navneet Jha"
git config --global
git config --global core.editor "code --wait" (if code is shortcut for VSCode)
git config --global -e (edit the config in default editor)
git config --global core.autocrlf xxx [xxx:true for windows, input for mac/linux] 
	On windows end of line --> Carriage return (\r) and Line feed (\n)
	On mac end of line --> Line feed (\n)

Initialize Git

git init : creates .git file in the local repository

It stores information about the repository history

Git Workflow

Local repo --> Staging Area (Index) [Add] --> Commit to Git [Commit]

Git Helpful commands

[Add] : Add files to the staging area

	git add -A or git add . (add all files)
	git add *.txt (add all .txt files)
	git add file1.txt file2.txt (add multiple files)

[Commit] : Commit our snapshot available in the staging area

	git commit -m "Commit message"
	git commit [Enter] --> will open default editor and enter comment there in case of large comments

[Status] : Check status of the local repository

	git status
	git status -s (for short status : AB filename , A: staging area, B: local)

To skip the staging step:

git commit -a -m "Commit message"

To view files in the staging area:

git ls-files

To remove a file from both local directory and staging area

git rm file.txt (multiple files or pattern also allowed)

To rename a file in both local directory and staging area

git mv previousfilename newfilename

.gitignore file contains files/directory/patterns to ignore staging.
If file is already included once, it will not be ignored even if it is added to the .gitignore
Solution : Remove the file from the staging area

To remove the file from the staging area only

git rm --cached filename
git rm --cached -r directoryname
git restore --staged . (filename/filenames/pattern)

To see the differences in the last commit and staging area

git diff --staged

To see the differences in the staging area and local

git diff

To see the history (Author, time and commit comment)

git log 
git log --online (only commit id and message)
git log --online --reverse (see logs in reverse order)
git show commitid
git show HEAD~X (see X distance commit details from HEAD)
git ls-tree commitid/HEAD~X (see all files at a particular commitid)
30 views0 comments

Recent Posts

See All