Day 20: Mastering Git: The Ultimate Cheat Sheet for Efficient Version Control
Master Git with the Ultimate Cheat Sheet

Day 20: Mastering Git: The Ultimate Cheat Sheet for Efficient Version Control

1. Uses of Git

  • Git is used for tracking changes in source code repositories, ensuring transparent version control.
  • As a distributed version control tool, Git facilitates efficient source code management across teams and repositories.
  • Enables seamless collaboration by allowing multiple developers to concurrently work on projects and contribute to shared repositories.
  • With its support for thousands of parallel branches, Git enables non-linear development workflows, empowering teams to explore diverse ideas and experiment with different features simultaneously.

2. Features of Git

  • Free and open-source
  • Tracks history
  • Supports non-linear development
  • Creates backup
  • Scalable
  • Supports collaboration
  • Branching is easier
  • Distributed development.

3. Git WorkFlow

The following image shows the git workflow diagram:

            +---------------------+
            |       Working       |
            |       Directory     |
            |                     |
            |   Your Project's    |
            |       Files         |
            +----------+----------+
                       |
                       |
                       v
            +----------+----------+
            |      Staging Area    |
            |     (Index)          |
            |                     |
            |   Changes to be      |
            |   committed          |
            +----------+----------+
                       |
                       v
            +----------+----------+
            |      Git Directory   |
            |                     |
            |   Repository Data   |
            |   (Commits, Branches,|
            |   Tags, etc.)       |
            +---------------------+
        

In Git, the workflow is mainly divided into three areas -

  • Working directory - This represents the directory on your local system where you have your project files. These files are editable and represent the current state of your project.
  • Staging area (Index) - The staging area is where you prepare changes before committing them to the Git repository. Files are added to the staging area using git add command.
  • Git directory or repository - This is where Git stores all the metadata and object database for your project. It contains the complete history of your project, including commits, branches, tags, etc. The .git directory at the root of your project represents the Git directory.

Git configuration:

Git config

Get and set configuration variables that control all facts of how Git looks and operates.

Set the name:

$ git config --global user.name "User name"        

Set the email:

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

Set the default editor:

$ git config --global core.editor Vim        

Check the setting:

$ git config -list        

Creating Snapshot


Initializing a repository:

$ git init      --> Initializing a repository        

Staging files:

$ git add file1.py  ---> Stages a single file

$ git add file1.py file2.py  ---> Stages multiple files 

$ git add *.py  ---> Stages with a pattern 

$ git add .      ---> Stages the current directory and all its content                                    

Viewing the status:

$ git status    ---> Full status

$ git status -s ---> Short status        

Committing the staged files:

$ git commit -m “Message” --->Commits with a one-line message					    

$ git commit           --->Opens the default editor to type a long message                                                       

Stage all modified files:

$ git commit -am “Message”  --> shorthand way to stage all modified files           
                                and commit them with a message in Git        

Removing files:

$ git rm file1.py          ---> Removes from working directory and staging 
                                area

$ git rm --cached file1.py ---> Removes from staging area only        

Renaming or moving files:

$ git mv file1.py file1.txt        

Unstaging files (undoing git add):

$ git restore --staged file.py    ---> Copies the last version of file.js  
                                       from repo to index                                                                 

Discarding local changes:

$ git restore file.py           ---> Copies file.js from index to working   
                                     directory

$ git restore file1.py file2.py ---> Restores multiple files in working   
                                     directory

$ git restore . git clean -fd   ---> Discards all local changes (except  
                                     untracked files)

$ git clean -fd                 ---> Removes all untracked files        

Restoring an earlier version of a file:

$ git restore --source=HEAD~2 file.py        

Viewing the staged/unstaged changes:

$ git diff                        ---> Shows unstaged changes

$ git diff  --staged              ---> Shows staged changes

$ git diff  --cached              ---> Shows staged changes        

Viewing the history:

$ git log            ---> Full history

$ git log --oneline  ---> Summary of commit in oneline

$ git log --reverse  ---> Lists the commits from the oldest to the newest

$ git log --all      ---> Display the commit history from all branches and 
                          tags, both local and remote branches.   

$ git log --graph    ---> commit history in a more visually informative   
                          way, showing the relationships between commits  
                          and branches using ASCII art.

$ git log --decorate --> adds decoration to commits, indicating which 
                         branches or tags point to specific commits.        

Can combine the above log commands to view the commits in a effective manner like:

ex:       $ git log --all --graph --decorate        

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.py ---> Shows the version of file.py stored in the  
                             last commit        

Unstaging files (undoing git add):

$ git restore --staged file.py? ---> Copies the last version of file.py 
                                     from repo to index        

Discarding local changes:

$ git restore file.py           ---> Copies file.py from index to working 
                                     directory

$ git restore file1.py file2.py ---> Restores multiple files in working 
                                     directory

$ git restore .                 ---> Discards all local changes (except 
                                     untracked files)

$ git clean -fd                 ---> Removes all untracked files        

Restoring an earlier version of a file:

$ git restore --source=HEAD~2 file.py        

Browsing History


Viewing the history:

$ git log --stat     ---> Shows the list of modified files

$ git log --patch    ---> Shows the actual changes (patches)        

Filtering the history:

$ git log -3                      ---> Shows the last 3 entries

$ git log --author=“priyanka”         

$ git log --before=“2023-08-17”   

$ git log --after=“one week ago”  

$ git log --grep=“GUI”            ---> Commits with “GUI” in their message

$ git log -S“GUI”                 ---> Commits with “GUI” in their patches

$ git log hash1..hash2            ---> Range of commits

$ git log file.txt                ---> Commits that touched file.txt        

Formatting the log output:

$ git log --pretty=format:'%an committed %H'        

Creating an alias:

$ git config --global alias.lg 'log --oneline'        

Viewing a commit

$ git show HEAD~2        

$ git show HEAD~2:file1.txt   ---> Shows the version of file stored in 
                                   this commit        

Comparing commits

$ git diff HEAD~2 HEAD            ---> Shows the changes between two 
                                        commits

$ git diff HEAD~2 HEAD file.txt   ---> Changes to file.txt only        

Checking out a commit

$ git checkout ef47ed       ---> Checks out the given commit

$ git checkout master       ---> Checks out the master branch        

Finding a bad commit

$ git bisect start       

$ git bisect bad          ---> Marks the current commit as a bad commit

$ git bisect good de14180 ---> Marks the given commit as a good commit

$ git bisect reset        ---> Terminates the bisect session        

Finding contributors

$ git shortlog        

Viewing the history of a file

$ git log file.txt          ---> Shows the commits that touched file.txt

$ git log --stat file.txt   ---> Shows statistics (the number of changes) 
                                 for file.txt

$ git log --patch file.txt  ---> Shows the patches (changes) applied to 
                                 file.txt        

Finding the author of lines

$ git blame file.txt????? ---> Shows?the?author?of?each?line?in?file.txt        

Tagging

$ git tag v1.0         ---> Tags the last commit as v1.0

$ git tag v1.0 6t7a828 ---> Tags an earlier commit

$ git tag              ---> Lists all the tags

$ git tag -d v1.0      ---> Deletes the given tag        

Branching & Merging


Managing branches

$ git branch test    ---> Creates a new branch called test
or
$ git checkout -b test   ---> Creates a new branch called test

$ git checkout test  ---> Switches to the test branch

$ git switch test    ---> Same as the above

$ git switch -C test ---> Creates and switches

$ git branch -d test ---> Deletes the test branch        

Comparing branches

$ git log master..test    ---> Lists the commits in the test branch 
                                 not in master

$ git diff master..test   ---> Shows the summary of changes        

Stashing:

In Git, "stashing" refers to temporarily shelving changes that are not ready to be committed. It allows you to save your work in progress and switch to another task or branch without committing incomplete changes to the repository.

In a single line: Stashing in Git is the process of temporarily storing changes that are not yet ready to be committed.

$ git stash push -m “New tax rules” ---> Creates a new stash

$ git stash list                    ---> Lists all the stashes

$ git stash show stash@{1}          ---> Shows the given stash

$ git stash show 1                  ---> shortcut for stash@{1}

$ git stash apply 1                 ---> Applies the given stash to the 
                                         working dir

$ git stash drop 1                  ---> Deletes the given stash

$ git stash clear                   ---> Deletes all the stashes        

Merging

$ git merge test            ---> Merges the test branch into the 
                                 current branch

$ git merge --no-ff test    ---> Creates a merge commit even if FF is 
                                   possible

$ git merge --squash test   ---> Performs a squash merge

$ git merge --abort         ---> Aborts the merge        

Viewing the merged branches

$ git branch --merged      ---> Shows the merged branches

$ git branch --no-merged   ---> Shows the unmerged branches        

Rebasing

$ git rebase master?? ---> Changes?the?base?of?the?current?branch        

Cherry picking

$ git cherry-pick ef47ed???---> Applies?the?given?commit?on?the?
                                 current?branch        

Collaboration


Cloning a repository

$ git clone url        ---> create a copy of the targeted repository                 
                            locally from remote         

Syncing with remotes

$ git fetch origin master  ---> Fetches master from origin

$ git fetch origin         ---> Fetches all objects from origin 

$ git fetch                ---> Shortcut for “git fetch origin” 

$ git pull                 ---> Fetch + merge

$ git push origin master   ---> Pushes master to origin

$ git push                 ---> Shortcut for “git push origin master”        

Sharing tags

$ git push origin v1.0            ---> Pushes tag v1.0 to origin

$ git push origin —delete v1.0        

Sharing branches

$ git branch -r                 ---> Shows remote tracking branches

$ git branch -vv                ---> Shows local & remote tracking 
                                      branches

$ git push -u origin test     ---> Pushes test to origin

$ git push -d origin test     ---> Removes test from origin        

Managing remotes:

$ git remote                    ---> Shows remote repos

$ git remote add upstream url   ---> Adds a new remote called upstream

$ git remote rm upstream        ---> Remotes upstream        

Rewriting History


Undoing commits:

$ git reset --soft HEAD^     ---> Undo the last commit, keeps changed                                                                 
                                  staged

$ git reset --mixed HEAD^    ---> Unstages the changes from last commit, 
                                  keeps changes in Working directory

$ git reset --hard HEAD^     ---> Discards any changes made after commit 
                                  4in both staging and working directory        

Reverting commits:

$ git revert 755656ea         ---> Reverts the given commit

$ git revert HEAD~3..        ---> Reverts the last three commits

$ git revert --no-commit  HEAD~3        

Recovering lost commits:

$ git reflog              ---> Shows the history of HEAD

$ git reflog show test    ---> Shows the history of test pointer        

Amending the last commit:

$ git commit --amend        

Interactive rebasing:

$ git rebase -i HEAD~5        


要查看或添加评论,请登录

Naga Priyanka Kanna的更多文章

社区洞察

其他会员也浏览了