How to learn Git in 15 minutes? List of most basic git commands

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git is easy to learn and has a tiny footprint with lightning fast performance. Every dev has a working copy of the code and full change history on their local machine

Here are some most important and useful GIT commands that will surely help you.

Git Commands:

  • This command sets the author name and email address respectively to be used with your commits.
$ git config –global user.name "[name]" 
$ git config –global user.email "[email address]"
  • This command is used to start new repository.
$ git init [repository name]
  • This command is used to obtain a repository from an existing URL.
$ git clone [url]  
  • This command records or snapshots the file permanently in the version history.
$ git commit -m "[Type in the commit message]"
  • This command lists all the files that have to be committed.
$ git status 
  • This command shows the metadata and content changes of the specified commit.
$ git show [commit]
  • These commands lists, creates and delete branch respectively.
$ git branch  
$ git branch [branch name]  
$ git branch -d [branch name]  
  • This command is used to connect your local repository to the remote server.
$ git remote add [variable name] [Remote Repo Link]  

  • This command sends the committed changes of master branch to your remote repository.
$ git push [variable name] master  
  • This command sends the branch commits to your remote repository.
$ git push [variable name] [branch]
  • This command pushes all branches to your remote repository.
$ git push –all [variable name]
  • This command deletes a branch on your remote repository.
$ git push [variable name] :[branch name]
  • This command fetches and merges changes on the remote server to your working directory
$ git pull [Repository Link]

If you want more commands with examples, please let us know in the comments below.

More Useful commands:

  • Checks if sha is in production.
$ git tag --contains [sha]
  • Number of commits by author.
$ git shortlog -s --author 'Author Name'
  • List of authors and commits to repository sorted alphabetically.
$ git shortlog -s -n
  • See differences in file before committing / compare recently edited file with its last committed state
$ git diff path/to/file.txt
  • Undo local changes to a file.
$ git checkout -- filename
  • Undo/revert last commit
git revert HEAD^
  • Remove last commit from history (WITHOUT keeping changes)
$ git reset --hard HEAD~
  • Remove last commit from history (WITH keeping changes)
$ git reset HEAD~
  • Shows number of lines added or removed from repository by an author since some time in the past.
$ git log --author="Author name" --pretty=tformat: --numstat --since=month | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'

referenceS:

https://gist.github.com/davfre/8313299#undoing-previous-actions

https://stackoverflow.com/questions/8903953/how-to-revert-last-commit-and-remove-it-from-history

https://github.com/bpassos/git-commands#committing-files

Magento 2 cli commands [continued]

1. Setting GD2 as defualt image adapter from command line 

Magento >= 2.2
php bin/magento config:set dev/image/default_adapter GD2
php bin/magento cache:clean
Magento >= 2.2
 mysql --host=(host) --user=(user) --password=(password) -D (database) -e "\ UPDATE core_config_data SET value = 'GD2' WHERE 'dev/image/default_adapter' = path; \"; 

 2. Disable Minify CSS Files option from command line

Magento >= 2.2
php bin/magento config:set dev/css/minify_files 0
php bin/magento cache:clean
Magento < 2.2
mysql --host= --user= --password= -D  -e "\ UPDATE core_config_data SET value = 0 WHERE 'dev/css/minify_files' = path; \";
php bin/magento cache:clean

3. Disable Merge CSS Files option from command line

 

php bin/magento config:set dev/css/merge_css_files 0
php bin/magento cache:clean
Magento < 2.2
mysql --host= --user= --password= -D  -e "\ UPDATE core_config_data SET value = 0 WHERE 'dev/css/merge_css_files' = path; \";
php bin/magento cache:clean

[SOLVED] PHP must be run as a CLI application error : Content-type: text/html; charset=UTF-8

php bin/magento cache:clean
Content-type: text/html; charset=UTF-8
bin/magento must be run as a CLI application

as root user, run the following commands (For php v7.0)

# yum install ea-php70
# source /opt/cpanel/ea-php70/enable

The solution involves fixing php variable’s path. We need to find which php is being used right now, you can find out by

$ which php
$ php -v

To fix this PHP cli error, see how to edit php in bash profile (.bashrc)

Magento 2 all cli commands

[~/www]# php bin/magento list
Magento CLI version 2.2.4
Usage:
command [options] [arguments]
Options:
-h, --help            Display this help message
-q, --quiet           Do not output any message
-V, --version         Display this application version
--ansi            Force ANSI output
--no-ansi         Disable ANSI output
-n, --no-interaction  Do not ask any interactive question
-v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
help                                     Displays help for a command
list                                     Lists commands
admin
admin:user:create                        Creates an administrator
admin:user:unlock                        Unlock Admin Account
app
app:config:dump                          Create dump of application
app:config:import                        Import data from shared configuration files to appropriate data storage
cache
cache:clean                              Cleans cache type(s)
cache:disable                            Disables cache type(s)
cache:enable                             Enables cache type(s)
cache:flush                              Flushes cache storage used by cache type(s)
cache:status                             Checks cache status
catalog
catalog:images:resize                    Creates resized product images
catalog:product:attributes:cleanup       Removes unused product attributes.
config
config:sensitive:set                     Set sensitive configuration values
config:set                               Change system configuration
config:show                              Shows configuration value for given path. If path is not specified, all saved values will be shown
cron
cron:install                             Generates and installs crontab for current user
cron:remove                              Removes tasks from crontab
cron:run                                 Runs jobs by schedule
customer
customer:hash:upgrade                    Upgrade customer's hash according to the latest algorithm
deploy
deploy:mode:set                          Set application mode.
deploy:mode:show                         Displays current application mode.
dev
dev:di:info                              Provides information on Dependency Injection configuration for the Command.
dev:profiler:disable                     Disable the profiler.
dev:profiler:enable                      Enable the profiler.
dev:query-log:disable                    Disable DB query logging
dev:query-log:enable                     Enable DB query logging
dev:source-theme:deploy                  Collects and publishes source files for theme.
dev:template-hints:disable               Disable frontend template hints. A cache flush might be required.
dev:template-hints:enable                Enable frontend template hints. A cache flush might be required.
dev:tests:run                            Runs tests
dev:urn-catalog:generate                 Generates the catalog of URNs to *.xsd mappings for the IDE to highlight xml.
dev:xml:convert                          Converts XML file using XSL style sheets
i18n
i18n:collect-phrases                     Discovers phrases in the codebase
i18n:pack                                Saves language package
i18n:uninstall                           Uninstalls language packages
import
import:categories                        Run category importer script
indexer
indexer:info                             Shows allowed Indexers
indexer:reindex                          Reindexes Data
indexer:reset                            Resets indexer status to invalid
indexer:set-mode                         Sets index mode type
indexer:show-mode                        Shows Index Mode
indexer:status                           Shows status of Indexer
info
info:adminuri                            Displays the Magento Admin URI
info:backups:list                        Prints list of available backup files
info:currency:list                       Displays the list of available currencies
info:dependencies:show-framework         Shows number of dependencies on Magento framework
info:dependencies:show-modules           Shows number of dependencies between modules
info:dependencies:show-modules-circular  Shows number of circular dependencies between modules
info:language:list                       Displays the list of available language locales
info:timezone:list                       Displays the list of available timezones
maintenance
maintenance:allow-ips                    Sets maintenance mode exempt IPs
maintenance:disable                      Disables maintenance mode
maintenance:enable                       Enables maintenance mode
maintenance:status                       Displays maintenance mode status
module
module:disable                           Disables specified modules
module:enable                            Enables specified modules
module:status                            Displays status of modules
module:uninstall                         Uninstalls modules installed by composer
sampledata
sampledata:deploy                        Deploy sample data modules
sampledata:remove                        Remove all sample data packages from composer.json
setup
setup:backup                             Takes backup of Magento Application code base, media and database
setup:config:set                         Creates or modifies the deployment configuration
setup:cron:run                           Runs cron job scheduled for setup application
setup:db-data:upgrade                    Installs and upgrades data in the DB
setup:db-schema:upgrade                  Installs and upgrades the DB schema
setup:db:status                          Checks if DB schema or data requires upgrade
setup:di:compile                         Generates DI configuration and all missing classes that can be auto-generated
setup:install                            Installs the Magento application
setup:performance:generate-fixtures      Generates fixtures
setup:rollback                           Rolls back Magento Application codebase, media and database
setup:static-content:deploy              Deploys static view files
setup:store-config:set                   Installs the store configuration. Deprecated since 2.2.0. Use config:set instead
setup:uninstall                          Uninstalls the Magento application
setup:upgrade                            Upgrades the Magento application, DB data, and schema
store
store:list                               Displays the list of stores
store:website:list                       Displays the list of websites
theme
theme:uninstall                          Uninstalls theme
varnish
varnish:vcl:generate                     Generates Varnish VCL and echos it to the command line

How to update Magento community edition using CLI

composer require magento/product-community-edition 2.2.6 --no-update
composer update
rm -rf var/di var/generation var/cache var/view_preprocessed generated/*
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento indexer:reindex
php bin/magento deploy:mode:set production

if you receive any errors post installation, try this hard fix

rm -rf app/code/Magento/ var/* vendor/*
chmod 777 -R *
composer update && composer install
php -f bin/magento setup:static-content:deploy
find . -type d -exec chmod 775 {}
find . -type f -exec chmod 660 {}
chmod u+x bin/magento
bin/magento maintenance:disable
bin/magento cache:clean

How to Update Magento 2.1x to latest version via SSH

composer require magento/product-community-edition 2.1.9 --no-update
composer update
rm -rf var/di/* var/generation/* var/cache/* var/log/* var/page_cache/*
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy
php bin/magento indexer:reindex