1603047600
The hotly anticipated release of blockbuster video game FIFA 21 on Oct. 6, along with the return of professional play, are giving soccer fans reason to celebrate. And, unsurprisingly, cybercriminals are already figuring out how to capitalize.
A report from researcher Christopher Boyd at Malwarebytes Labs outlined the various ways scammers are tapping into the oversized audience of FIFA 21 to turn a quick buck, including leveraging in-game goods and rewards.
Soccer is the world’s most popular sport, drawing in an estimated 3.5 billion fans all over the globe. Bloomberg reported during the last World Cup that four out of 10 people in the world consider themselves to be fans of the game.
That enthusiasm extends to the blockbuster video gaming franchise, FIFA, named after the sport’s international rules organization. The brand is recognized by the Guinness Book of World Records as the top selling sports video game of all time, with more than 280 million copies of the game sold in at least 51 countries.
The fact that the pandemic has slowed down in-person play (while one of the sport’s brightest stars, Cristiano Ronaldo, tested positive for COVID-19 earlier this week) hasn’t done anything to deter people from immersing themselves in the “beautiful game.” And it’s exactly that devotion and online interest that cybercriminals are leveraging to trick fans into their traps.
In his blog post explanation of his findings, Boyd explained that fraudsters are finding an easy hunting ground through a game mode called FIFA Ultimate Team (FUT).
Within this mode, players can earn “coins” which are used within the game to buy “cards,” which Boyd described as “the lifeblood of the game.”
__Phishing page example. Source: Malwarebytes
“So far, so good…and essentially harmless,” he continued. “Unfortunately, the monetized aspects of the game away from the screen contributes to scammers wanting a piece of the action.”
He pointed out there’s something called “FIFA points” which can be bought with real-life money within the game and from legit third parties. This is exactly the type of scenario that tends to grab the attention of fraudsters, he pointed out.
#vulnerabilities #web security #attack #coins #covid positive #cristiano ronaldo #cyberattack #cybersecurity #fifa #fifa 21 #fifa 21 game #fifa cards #fifa fans #fifa game #fifa game scams #fifa points #fifa world cup #fraud #fut #in-game purchases #lionel messi #phishing #security breach #soccer #video game launch #world cup #world’s most popular sport
1603047600
The hotly anticipated release of blockbuster video game FIFA 21 on Oct. 6, along with the return of professional play, are giving soccer fans reason to celebrate. And, unsurprisingly, cybercriminals are already figuring out how to capitalize.
A report from researcher Christopher Boyd at Malwarebytes Labs outlined the various ways scammers are tapping into the oversized audience of FIFA 21 to turn a quick buck, including leveraging in-game goods and rewards.
Soccer is the world’s most popular sport, drawing in an estimated 3.5 billion fans all over the globe. Bloomberg reported during the last World Cup that four out of 10 people in the world consider themselves to be fans of the game.
That enthusiasm extends to the blockbuster video gaming franchise, FIFA, named after the sport’s international rules organization. The brand is recognized by the Guinness Book of World Records as the top selling sports video game of all time, with more than 280 million copies of the game sold in at least 51 countries.
The fact that the pandemic has slowed down in-person play (while one of the sport’s brightest stars, Cristiano Ronaldo, tested positive for COVID-19 earlier this week) hasn’t done anything to deter people from immersing themselves in the “beautiful game.” And it’s exactly that devotion and online interest that cybercriminals are leveraging to trick fans into their traps.
In his blog post explanation of his findings, Boyd explained that fraudsters are finding an easy hunting ground through a game mode called FIFA Ultimate Team (FUT).
Within this mode, players can earn “coins” which are used within the game to buy “cards,” which Boyd described as “the lifeblood of the game.”
__Phishing page example. Source: Malwarebytes
“So far, so good…and essentially harmless,” he continued. “Unfortunately, the monetized aspects of the game away from the screen contributes to scammers wanting a piece of the action.”
He pointed out there’s something called “FIFA points” which can be bought with real-life money within the game and from legit third parties. This is exactly the type of scenario that tends to grab the attention of fraudsters, he pointed out.
#vulnerabilities #web security #attack #coins #covid positive #cristiano ronaldo #cyberattack #cybersecurity #fifa #fifa 21 #fifa 21 game #fifa cards #fifa fans #fifa game #fifa game scams #fifa points #fifa world cup #fraud #fut #in-game purchases #lionel messi #phishing #security breach #soccer #video game launch #world cup #world’s most popular sport
1667086140
This bundle provides a collection of annotations for Symfony2 Controllers, designed to streamline the creation of certain objects and enable smaller and more concise actions.
By default, all annotations are loaded, but any individual annotation can be completely disabled by setting to false active
parameter.
Default values are:
controller_extra:
resolver_priority: -8
request: current
paginator:
active: true
default_name: paginator
default_page: 1
default_limit_per_page: 10
entity:
active: true
default_name: entity
default_persist: true
default_mapping_fallback: false
default_factory_method: create
default_factory_mapping: true
form:
active: true
default_name: form
object_manager:
active: true
default_name: form
flush:
active: true
default_manager: default
json_response:
active: true
default_status: 200
default_headers: []
log:
active: true
default_level: info
default_execute: pre
ResolverEventListener is subscribed to
kernel.controller
event with priority -8. This element can be configured and customized withresolver_priority
config value. If you need to get ParamConverter entities, make sure that this value is lower than 0. The reason is that this listener must be executed always after ParamConverter one.
Entity provider
In some annotations, you can define an entity by several ways. This chapter is about how you can define them.
You can define an entity using its namespace. A simple new new()
be performed.
/**
* Simple controller method
*
* @SomeAnnotation(
* class = "Mmoreram\CustomBundle\Entity\MyEntity",
* )
*/
public function indexAction()
{
}
You can define an entity using Doctrine shortcut notations. With this format you should ensure that your Entities follow Symfony Bundle standards and your entities are placed under Entity/
folder.
/**
* Simple controller method
*
* @SomeAnnotation(
* class = "MmoreramCustomBundle:MyEntity",
* )
*/
public function indexAction()
{
}
You can define an entity using a simple config parameter. Some projects use parameters to define all entity namespaces (To allow overriding). If you define the entity with a parameter, this bundle will try to instance it with a simple new()
accessing directly to the container ParametersBag.
parameters:
#
# Entities
#
my.bundle.entity.myentity: Mmoreram\CustomBundle\Entity\MyEntity
/**
* Simple controller method
*
* @SomeAnnotation(
* class = "my.bundle.entity.myentity",
* )
*/
public function indexAction()
{
}
Controller annotations
This bundle provide a reduced but useful set of annotations for your controller actions.
Creates a Doctrine Paginator object, given a request and a configuration. This annotation just injects into de controller a new Doctrine\ORM\Tools\Pagination\Pagination
instance ready to be iterated.
You can enable/disable this bundle by overriding active
flag in configuration file config.yml
controller_extra:
pagination:
active: true
By default, if
name
option is not set, the generated object will be placed in a parameter named$paginator
. This behaviour can be configured usingdefault_name
in configuration.
This annotation can be configured with these sections
To create a new Pagination object you need to refer to an existing Entity. You can check all available formats you can define it just reading the Entity Provider section.
<?php
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* )
*/
public function indexAction(Paginator $paginator)
{
}
You need to specify Paginator annotation the page to fetch. By default, if none is specified, this bundle will use the default one defined in configuration. You can override in config.yml
controller_extra:
pagination:
default_page: 1
You can refer to an existing Request attribute using ~value~
format, to any $_GET
element by using format ?field?
or to any $_POST
by using format #field#
You can choose between Master Request or Current Request accessing to its attributes, by configuring the request value of the configuration.
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/{foo}
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* page = "~foo~"
* )
*/
public function indexAction(Paginator $paginator)
{
}
or you can hardcode the page to use.
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* page = 1
* )
*/
public function indexAction(Paginator $paginator)
{
}
You need to specify Paginator annotation the limit to fetch. By default, if none is specified, this bundle will use the default one defined in configuration. You can override in config.yml
controller_extra:
pagination:
default_limit_per_page: 10
You can refer to an existing Request attribute using ~value~
format, to any $_GET
element by using format ?field?
or to any $_POST
by using format #field#
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/{foo}/{limit}
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* page = "~foo~",
* limit = "~limit~"
* )
*/
public function indexAction(Paginator $paginator)
{
}
or you can hardcode the page to use.
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* page = 1,
* limit = 10
* )
*/
public function indexAction(Paginator $paginator)
{
}
You can order your Pagination just defining the fields you want to orderBy and the desired direction. The orderBy
section must be defined as an array of arrays, and each array should contain these positions:
x
)use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* orderBy = {
* {"x", "createdAt", "ASC"},
* {"x", "updatedAt", "DESC"},
* {"x", "id", 1, {
* 0 => "ASC",
* 1 => "DESC",
* }},
* }
* )
*/
public function indexAction(Paginator $paginator)
{
}
With the third and fourth value you can define a map where to match your own direction nomenclature with DQL one. DQL nomenclature just accept ASC for Ascendant and DESC for Descendant.
This is very useful when you need to match a url format with the DQL one. You can refer to an existing Request attribute using ~value~
format, to any $_GET
element by using format ?field?
or to any $_POST
by using format #field#
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/order/{field}/{direction}
*
* For example, some matchings...
*
* /myroute/paginate/order/id/1 -> ORDER BY id DESC
* /myroute/paginate/order/enabled/0 - ORDER BY enabled ASC
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* orderBy = {
* {"x", "createdAt", "ASC"},
* {"x", "updatedAt", "DESC"},
* {"x", "~field~", ~direction~, {
* 0 => "ASC",
* 1 => "DESC",
* }},
* }
* )
*/
public function indexAction(Paginator $paginator)
{
}
The order of the definitions will alter the order of the DQL query.
You can define some where statements in your Paginator. The wheres
section must be defined as an array of arrays, and each array should contain these positions:
x
)use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* wheres = {
* {"x", "enabled", "=", true},
* {"x", "age", ">", 18},
* {"x", "name", "LIKE", "Eferv%"},
* }
* )
*/
public function indexAction(Paginator $paginator)
{
}
You can refer to an existing Request attribute using ~value~
format, to any $_GET
element by using format ?field?
or to any $_POST
by using format #field#
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* This Controller matches pattern /myroute/{field}
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* wheres = {
* {"x", "name", "LIKE", "~field~"},
* }
* )
*/
public function indexAction(Paginator $paginator)
{
}
You can use as well this feature for optional filtering by setting the last position to true
. In that case, if the filter value is not found, such line will be ignored.
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* This Controller matches pattern /myroute?query=name%
* This Controller matches pattern /myroute as well
*
* In both cases this will work. In the first case we will apply the where line
* in the paginator. In the second case, we wont.
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* wheres = {
* {"x", "name", "LIKE", "?query?", true},
* }
* )
*/
public function indexAction(Paginator $paginator)
{
}
You can also define some fields to not null. Is same as wheres
section, but specific for NULL assignments. The notNulls
section must be defined as an array of arrays, and each array should contain these positions:
x
)use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* notNulls = {
* {"x", "enabled"},
* {"x", "deleted"},
* }
* )
*/
public function indexAction(Paginator $paginator)
{
}
You can do some left joins in this section. The leftJoins
section must be defined as an array of array, where each array can have these fields:
x
)use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* leftJoins = {
* {"x", "User", "u", true},
* {"x", "Address", "a", true},
* {"x", "Cart", "c"},
* }
* )
*/
public function indexAction(Paginator $paginator)
{
}
You can do some inner joins in this section. The innerJoins
section must be defined as an array of array, where each array can have these fields:
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* innerJoins = {
* {"x", "User", "u", true},
* {"x", "Address", "a", true},
* {"x", "Cart", "c"},
* }
* )
*/
public function indexAction(Paginator $paginator)
{
}
A nice feature of this annotation is that you can also inject into your controller a Mmoreram\ControllerExtraBundle\ValueObject\PaginatorAttributes
instance with some interesting information about your pagination.
To inject this object you need to define the "attributes" annotation field with the method parameter name.
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
use Mmoreram\ControllerExtraBundle\ValueObject\PaginatorAttributes;
/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/
*
* @CreatePaginator(
* attributes = "paginatorAttributes",
* entityNamespace = "MmoreramCustomBundle:User",
* page = 1,
* limit = 10
* )
*/
public function indexAction(
Paginator $paginator,
PaginatorAttributes $paginatorAttributes
)
{
$currentPage = $paginatorAttributes->getCurrentPage();
$totalElements = $paginatorAttributes->getTotalElements();
$totalPages = $paginatorAttributes->getTotalPages();
$limitPerPage = $paginatorAttributes->getLimitPerPage();
}
This is a completed example and its DQL resolution
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
/**
* Simple controller method
*
* This Controller matches pattern /paginate/nb/{limit}/{page}
*
* Where:
*
* * limit = 10
* * page = 1
*
* @CreatePaginator(
* entityNamespace = "ControllerExtraBundle:Fake",
* page = "~page~",
* limit = "~limit~",
* orderBy = {
* { "x", "createdAt", "ASC" },
* { "x", "updatedAt", "DESC" },
* { "x", "id", "0", {
* "1" = "ASC",
* "2" = "DESC",
* }}
* },
* wheres = {
* { "x", "enabled" , "=", true }
* },
* leftJoins = {
* { "x", "relation", "r" },
* { "x", "relation2", "r2" },
* { "x", "relation5", "r5", true },
* },
* innerJoins = {
* { "x", "relation3", "r3" },
* { "x", "relation4", "r4", true },
* },
* notNulls = {
* {"x", "address1"},
* {"x", "address2"},
* }
* )
*/
public function indexAction(Paginator $paginator)
{
}
The DQL generated by this annotation is
SELECT x, r4, r5
FROM Mmoreram\\ControllerExtraBundle\\Tests\\FakeBundle\\Entity\\Fake x
INNER JOIN x.relation3 r3
INNER JOIN x.relation4 r4
LEFT JOIN x.relation r
LEFT JOIN x.relation2 r2
LEFT JOIN x.relation5 r5
WHERE enabled = ?where0
AND x.address1 IS NOT NULL
AND x.address2 IS NOT NULL
ORDER BY createdAt ASC, id ASC
This annotation can create a PagerFanta instance if you need it. You only have to define your parameter as such, and the annotation resolver will wrap your paginator with a Pagerfanta object instance.
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
use Pagerfanta\Pagerfanta;
/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* page = 1,
* limit = 10
* )
*/
public function indexAction(Pagerfanta $paginator)
{
}
This annotation can create a KNPPaginator instance if you need it. You only have to define your parameter as such, and the annotation resolver will wrap your paginator with a KNPPaginator object instance.
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
use Knp\Component\Pager\Pagination\PaginationInterface;
/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/
*
* @CreatePaginator(
* entityNamespace = "MmoreramCustomBundle:User",
* page = 1,
* limit = 10
* )
*/
public function indexAction(PaginationInterface $paginator)
{
}
Loads an entity from your database, or creates a new one.
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;
/**
* Simple controller method
*
* @Entity(
* namespace = "MmoreramCustomBundle:User",
* name = "user"
* )
*/
public function indexAction(User $user)
{
}
By default, if
name
option is not set, the generated object will be placed in a parameter named$entity
. This behaviour can be configured usingdefault_name
in configuration.
You can also use setters in Entity annotation. It means that you can simply call entity setters using Request attributes.
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\Address;
use Mmoreram\ControllerExtraBundle\Entity\User;
/**
* Simple controller method
*
* @Entity(
* namespace = "MmoreramCustomBundle:Address",
* name = "address"
* )
* @Entity(
* namespace = "MmoreramCustomBundle:User",
* name = "user",
* setters = {
* "setAddress": "address"
* }
* )
*/
public function indexAction(Address $address, User $user)
{
}
When User
instance is built, method setAddress
is called using as parameter the new Address
instance.
New entities are just created with a simple new()
, so they are not persisted. By default, they will be persisted using configured manager, but you can disable this feature using persist
option.
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;
/**
* Simple controller method
*
* @Entity(
* namespace = "MmoreramCustomBundle:User",
* name = "user",
* persist = false
* )
*/
public function indexAction(User $user)
{
}
When you define a new Entity annotation, you can also request the mapped entity given a map. It means that if a map is defined, this bundle will try to request the mapped instance satisfying it.
The keys of the map represent the names of the mapped fields and the values represent their desired values. Remember than you can refer to any Request attribute by using format ~field~
, to any $_GET
element by using format ?field?
or to any $_POST
by using format #field#
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;
/**
* Simple controller method
*
* This Controller matches pattern /user/edit/{id}/{username}
*
* @Entity(
* namespace = "MmoreramCustomBundle:User",
* name = "user",
* mapping = {
* "id": "~id~",
* "username": "~username~"
* }
* )
*/
public function indexAction(User $user)
{
}
In this case, you will try to get the mapped instance of User with passed id. If some mapping is defined and any entity is found, a new EntityNotFoundException` is thrown.
So what if one ore more than one mapping references are not found? For example, you're trying to map the {id} parameter from your route, but this parameter is not even defined. Whan happens here? Well, you can assume then that you want to pass a new entity instance by using the mappingFallback.
By default, if
mapping_fallback
option is not set, the used value will be the parameterdefault_mapping_fallback
defined in configuration. By default this value isfalse
Don't confuse with the scenario where you're looking for an entity in your database, all mapping references have been resolved, and the entity is not found. In that case, a common "EntityNotFound" exception will be thrown by Doctrine.
Lets see an example. Because we have enabled the mappingFallback, and because the mapping definition does not match the assigned route, we will return a new empty User entity.
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;
/**
* Simple controller method
*
* This Controller matches pattern /user/edit/{id}
*
* @LoadEntity(
* namespace = "MmoreramCustomBundle:User",
* name = "user",
* mapping = {
* "id": "~id~",
* "username": "~nonexisting~"
* },
* mappingFallback = true
* )
*/
public function indexAction(User $user)
{
// $user->getId() === null
}
By default, the Doctrine entity manager provides the right repository per each entity (not the default one, but the right specific one). Although, you can define a custom repository to be used in your annotation by using the repository configuration.
/**
* Simple controller method
*
* @CreateEntity(
* namespace = "MmoreramCustomBundle:User",
* mapping = {
* "id": "~id~",
* "username": "~username~"
* }
* repository = {
* "class" = "Mmoreram\CustomBundle\Repository\AnotherRepository",
* },
* )
*/
public function indexAction(User $user)
{
}
By default, the method findOneBy will always be used, unless you define another one.
/**
* Simple controller method
*
* @CreateEntity(
* namespace = "MmoreramCustomBundle:User",
* mapping = {
* "id": "~id~",
* "username": "~username~"
* }
* repository = {
* "class" = "Mmoreram\CustomBundle\Repository\AnotherRepository",
* "method" = "find",
* },
* )
*/
public function indexAction(User $user)
{
}
When the annotation considers that a new entity must be created, because no mapping information has been provided, or because the mapping fallback has been activated, by default a new instance will be created by using the namespace value.
This configuration block has three positions
You can define the factory with a simple namespace
/**
* Simple controller method
*
* @CreateEntity(
* namespace = "MmoreramCustomBundle:User",
* factory = {
* "class" = "Mmoreram\CustomBundle\Factory\UserFactory",
* "method" = "create",
* "static" = true,
* },
* )
*/
public function indexAction(User $user)
{
}
If you want to define your Factory as a service, with the possibility of overriding namespace, you can simply define service name. All other options have the same behaviour.
parameters:
#
# Factories
#
my.bundle.factory.user_factory: Mmoreram\CustomBundle\Factory\UserFactory
/**
* Simple controller method
*
* @CreateEntity(
* class = {
* "factory" = my.bundle.factory.user_factory,
* "method" = "create",
* "static" = true,
* },
* )
*/
public function indexAction(User $user)
{
}
If you do not define the method
, default one will be used. You can override this default value by defining new one in your config.yml
. Same with static
value
controller_extra:
entity:
default_factory_method: create
default_factory_static: true
Provides form injection in your controller actions. This annotation only needs a name to be defined in, where you must define namespace where your form is placed.
<?php
use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Symfony\Component\Form\AbstractType;
/**
* Simple controller method
*
* @CreateForm(
* class = "\Mmoreram\CustomBundle\Form\Type\UserType",
* name = "userType"
* )
*/
public function indexAction(AbstractType $userType)
{
}
By default, if
name
option is not set, the generated object will be placed in a parameter named$form
. This behaviour can be configured usingdefault_name
in configuration.
You can not just define your Type location using the namespace, in which case a new AbstractType element will be created. but you can also define it using service alias, in which case this bundle will return an instance using Symfony DI.
<?php
use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Symfony\Component\Form\AbstractType;
/**
* Simple controller method
*
* @CreateForm(
* class = "user_type",
* name = "userType"
* )
*/
public function indexAction(AbstractType $userType)
{
}
This annotation allows you to not only create an instance of FormType, but also allows you to inject a Form object or a FormView object
To inject a Form object you only need to cast method value as such.
<?php
use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Symfony\Component\Form\Form;
/**
* Simple controller method
*
* @CreateForm(
* class = "user_type",
* name = "userForm"
* )
*/
public function indexAction(Form $userForm)
{
}
You can also, using [SensioFrameworkExtraBundle][1]'s [ParamConverter][2], create a Form object with an previously created entity. you can define this entity using entity
parameter.
<?php
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;
use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Mmoreram\ControllerExtraBundle\Entity\User;
/**
* Simple controller method
*
* @Route(
* path = "/user/{id}",
* name = "view_user"
* )
* @ParamConverter("user", class="MmoreramCustomBundle:User")
* @CreateForm(
* class = "user_type",
* entity = "user"
* name = "userForm",
* )
*/
public function indexAction(User $user, Form $userForm)
{
}
To handle current request, you can set handleRequest
to true. By default this value is set to false
<?php
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;
use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Mmoreram\ControllerExtraBundle\Entity\User;
/**
* Simple controller method
*
* @Route(
* path = "/user/{id}",
* name = "view_user"
* )
* @ParamConverter("user", class="MmoreramCustomBundle:User")
* @CreateForm(
* class = "user_type",
* entity = "user"
* handleRequest = true,
* name = "userForm",
* )
*/
public function indexAction(User $user, Form $userForm)
{
}
You can also add as a method parameter if the form is valid, using validate
setting. Annotation will place result of $form->isValid()
in specified method argument.
<?php
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;
use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Mmoreram\ControllerExtraBundle\Entity\User;
/**
* Simple controller method
*
* @Route(
* path = "/user/{id}",
* name = "view_user"
* )
* @ParamConverter("user", class="MmoreramCustomBundle:User")
* @CreateForm(
* class = "user_type",
* entity = "user"
* handleRequest = true,
* name = "userForm",
* validate = "isValid",
* )
*/
public function indexAction(User $user, Form $userForm, $isValid)
{
}
To inject a FormView object you only need to cast method variable as such.
<?php
use Symfony\Component\Form\FormView;
use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
/**
* Simple controller method
*
* @CreateForm(
* class = "user_type",
* name = "userFormView"
* )
*/
public function indexAction(FormView $userFormView)
{
}
Flush annotation allows you to flush entityManager at the end of request using kernel.response event
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Flush;
/**
* Simple controller method
*
* @Flush
*/
public function indexAction()
{
}
If not otherwise specified, default Doctrine Manager will be flushed with this annotation. You can overwrite default Manager in your config.yml
file.
controller_extra:
flush:
default_manager: my_custom_manager
You can also override this value in every single Flush Annotation instance defining manager
value
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Flush;
/**
* Simple controller method
*
* @Flush(
* manager = "my_own_manager"
* )
*/
public function indexAction()
{
}
If you want to change default manager in all annotation instances, you should override bundle parameter in your config.yml
file.
controller_extra:
flush:
default_manager: my_own_manager
If any parameter is set, annotation will flush all. If you only need to flush one or many entities, you can define explicitly which entity must be flushed.
<?php
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Mmoreram\ControllerExtraBundle\Annotation\Flush;
use Mmoreram\ControllerExtraBundle\Entity\User;
/**
* Simple controller method
*
* @ParamConverter("user", class="MmoreramCustomBundle:User")
* @Flush(
* entity = "user"
* )
*/
public function indexAction(User $user)
{
}
You can also define a set of entities to flush
<?php
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Mmoreram\ControllerExtraBundle\Annotation\Flush;
use Mmoreram\ControllerExtraBundle\Entity\Address;
use Mmoreram\ControllerExtraBundle\Entity\User;
/**
* Simple controller method
*
* @ParamConverter("user", class="MmoreramCustomBundle:User")
* @ParamConverter("address", class="MmoreramCustomBundle:Address")
* @Flush(
* entity = {
* "user",
* "address"
* }
* )
*/
public function indexAction(User $user, Address $address)
{
}
If multiple @Mmoreram\Flush are defined in same action, last instance will overwrite previous. Anyway just one instance should be defined.
JsonResponse annotation allows you to create a Symfony\Component\HttpFoundation\JsonResponse
object, given a simple controller return value.
<?php
use Mmoreram\ControllerExtraBundle\Annotation\ToJsonResponse;
/**
* Simple controller method
*
* @ToJsonResponse
*/
public function indexAction(User $user, Address $address)
{
return array(
'This is my response'
);
}
By default, JsonResponse is created using default status
and headers
defined in bundle parameters. You can overwrite them in your config.yml
file.
controller_extra:
json_response:
default_status: 403
default_headers:
"User-Agent": "Googlebot/2.1"
You can also overwrite these values in each @JsonResponse
annotation.
<?php
use Mmoreram\ControllerExtraBundle\Annotation\ToJsonResponse;
/**
* Simple controller method
*
* @ToJsonResponse(
* status = 403,
* headers = {
* "User-Agent": "Googlebot/2.1"
* }
* )
*/
public function indexAction(User $user, Address $address)
{
return array(
'This is my response'
);
}
If an Exception is returned the response status is set by default to 500 and the Exception message is returned as response.
STATUS 500 Internal server error
{
message : 'Exception message'
}
In case we use a HttpExceptionInterface the use the exception status code as status code. In case we launch this exception
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
...
return new NotFoundHttpException('Resource not found');
We'll receive this response
STATUS 404 Not Found
{
message : 'Resource not found'
}
If the exception is being launched on an annotation (e.g. Entity annotation) remember to add the JsonResponse annotation at the beginning or at least before any annotation that could cause an exception.
If multiple @Mmoreram\JsonResponse are defined in same action, last instance will overwrite previous. Anyway just one instance should be defined.
Log annotation allows you to log any plain message before or after controller action execution
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Log;
/**
* Simple controller method
*
* @Log("Executing index Action")
*/
public function indexAction()
{
}
You can define the level of the message. You can define default one if none is specified overriding it in your config.yml
file.
controller_extra:
log:
default_level: warning
Every Annotation instance can overwrite this value using level
field.
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Log;
/**
* Simple controller method
*
* @Log(
* value = "Executing index Action",
* level = @Log::LVL_WARNING
* )
*/
public function indexAction()
{
}
Several levels can be used, as defined in [Psr\Log\LoggerInterface][6] interface
You can also define the execution of the log. You can define default one if none is specified overriding it in your config.yml
file.
controller_extra:
log:
default_execute: pre
Every Annotation instance can overwrite this value using level
field.
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Log;
/**
* Simple controller method
*
* @Log(
* value = "Executing index Action",
* execute = @Log::EXEC_POST
* )
*/
public function indexAction()
{
}
Several executions can be used,
The Get annotation allows you to get any parameter from the request query string.
For a GET
request like:
GET /my-page?foo=bar HTTP/1.1
You can can simply get the foo
var using the GET
annotation
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Get;
/**
* Simple controller method
*
* @Get(
* path = "foo"
* )
*/
public function indexAction($foo)
{
// Use the foo var
}
You can also customize the var name and the default value in case the var is not sent on the query string.
For a GET
request like:
GET /my-page HTTP/1.1
And this annotation
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Get;
/**
* Simple controller method
*
* @Get(
* path = "foo",
* name = "varName",
* default = 'bar',
* )
*/
public function indexAction($varName)
{
// This would print 'bar'
echo $varName;
}
The Post annotation allows you to get any parameter from the post request body.
For a POST
request like:
POST /my-page HTTP/1.1
foo=bar
You can can simply get the foo
var using the POST
annotation
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Post;
/**
* Simple controller method
*
* @Post(
* path = "foo"
* )
*/
public function indexAction($foo)
{
// Use the foo var
}
You can also customize the var name and the default value in case the var is not sent on the query string.
For a POST
request like:
POST /my-page HTTP/1.1
And this annotation
<?php
use Mmoreram\ControllerExtraBundle\Annotation\Post;
/**
* Simple controller method
*
* @Post(
* path = "foo",
* name = "varName",
* default = 'bar',
* )
*/
public function indexAction($varName)
{
// This would print 'bar'
echo $varName;
}
Custom annotations
Using this bundle you can now create, in a very easy way, your own controller annotation.
The annotation object. You need to define the fields your custom annotation will contain. Must extends Mmoreram\ControllerExtraBundle\Annotation\Annotation
abstract class.
<?php
namespace My\Bundle\Annotation;
use Mmoreram\ControllerExtraBundle\Annotation\Annotation;
/**
* Entity annotation driver
*
* @Annotation
* @Target({"METHOD"})
*/
final class MyCustomAnnotation extends Annotation
{
/**
* @var string
*
* Dummy field
*/
public $field;
/**
* Get Dummy field
*
* @return string Dummy field
*/
public function getField()
{
return $this->field;
}
}
Once you have defined your own annotation, you have to resolve how this annotation works in a controller. You can manage this using a Resolver. Must extend Mmoreram\ControllerExtraBundle\Resolver\AnnotationResolver;
abstract class.
<?php
namespace My\Bundle\Resolver;
use Symfony\Component\HttpFoundation\Request;
use Mmoreram\ControllerExtraBundle\Resolver\AnnotationResolver;
use Mmoreram\ControllerExtraBundle\Annotation\Annotation;
/**
* MyCustomAnnotation Resolver
*/
class MyCustomAnnotationResolver extends AnnotationResolver
{
/**
* Specific annotation evaluation.
*
* This method must be implemented in every single EventListener
* with specific logic
*
* All method code will executed only if specific active flag is true
*
* @param Request $request
* @param Annotation $annotation
* @param ReflectionMethod $method
*/
public function evaluateAnnotation(
Request $request,
Annotation $annotation,
ReflectionMethod $method
)
{
/**
* You can now manage your annotation.
* You can access to its fields using public methods.
*
* Annotation fields can be public and can be acceded directly,
* but is better for testing to use getters; they can be mocked.
*/
$field = $annotation->getField();
/**
* You can also access to existing method parameters.
*
* Available parameters are:
*
* # ParamConverter parameters ( See `resolver_priority` config value )
* # All method defined parameters, included Request object if is set.
*/
$entity = $request->attributes->get('entity');
/**
* And you can now place new elements in the controller action.
* In this example we are creating new method parameter
* called $myNewField with some value
*/
$request->attributes->set(
'myNewField',
new $field()
);
return $this;
}
}
This class will be defined as a service, so this method is computed just before executing current controller. You can also subscribe to some kernel events and do whatever you need to do ( You can check Mmoreram\ControllerExtraBundle\Resolver\LogAnnotationResolver
for some examples.
Once Resolver is done, we need to define our service as an Annotation Resolver. We will use a custom tag
.
parameters:
#
# Resolvers
#
my.bundle.resolver.my_custom_annotation_resolver.class: My\Bundle\Resolver\MyCustomAnnotationResolver
services:
#
# Resolvers
#
my.bundle.resolver.my_custom_annotation_resolver:
class: %my.bundle.resolver.my_custom_annotation_resolver.class%
tags:
- { name: controller_extra.annotation }
We need to register our annotation inside our application. We can just do it in the boot()
method of bundle.php
file.
<?php
namespace My\Bundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Doctrine\Common\Annotations\AnnotationRegistry;
/**
* MyBundle
*/
class ControllerExtraBundle extends Bundle
{
/**
* Boots the Bundle.
*/
public function boot()
{
$kernel = $this->container->get('kernel');
AnnotationRegistry::registerFile($kernel
->locateResource("@MyBundle/Annotation/MyCustomAnnotation.php")
);
}
}
Et voilà! We can now use our custom Annotation in our project controllers.
Author: mmoreram
Source Code: https://github.com/mmoreram/ControllerExtraBundle
License: MIT license
1659408900
TinyTDS - Simple and fast FreeTDS bindings for Ruby using DB-Library.
The TinyTDS gem is meant to serve the extremely common use-case of connecting, querying and iterating over results to Microsoft SQL Server or Sybase databases from Ruby using the FreeTDS's DB-Library API.
TinyTDS offers automatic casting to Ruby primitives along with proper encoding support. It converts all SQL Server datatypes to native Ruby primitives while supporting :utc or :local time zones for time-like types. To date it is the only Ruby client library that allows client encoding options, defaulting to UTF-8, while connecting to SQL Server. It also properly encodes all string and binary data. The motivation for TinyTDS is to become the de-facto low level connection mode for the SQL Server Adapter for ActiveRecord.
The API is simple and consists of these classes:
Installing with rubygems should just work. TinyTDS is currently tested on Ruby version 2.0.0 and upward.
$ gem install tiny_tds
If you use Windows, we pre-compile TinyTDS with static versions of FreeTDS and supporting libraries. If you're using RubyInstaller the binary gem will require that devkit is installed and in your path to operate properly.
On all other platforms, we will find these dependencies. It is recommended that you install the latest FreeTDS via your method of choice. For example, here is how to install FreeTDS on Ubuntu. You might also need the build-essential
and possibly the libc6-dev
packages.
$ apt-get install wget
$ apt-get install build-essential
$ apt-get install libc6-dev
$ wget http://www.freetds.org/files/stable/freetds-1.1.24.tar.gz
$ tar -xzf freetds-1.1.24.tar.gz
$ cd freetds-1.1.24
$ ./configure --prefix=/usr/local --with-tdsver=7.3
$ make
$ make install
Please read the MiniPortile and/or Windows sections at the end of this file for advanced configuration options past the following:
--with-freetds-dir=DIR
Use the freetds library placed under DIR.
Optionally, Microsoft has done a great job writing some articles on how to get started with SQL Server and Ruby using TinyTDS. Please checkout one of the following posts that match your platform.
TinyTDS is developed against FreeTDS 0.95, 0.99, and 1.0 current. Our default and recommended is 1.0. We also test with SQL Server 2008, 2014, and Azure. However, usage of TinyTDS with SQL Server 2000 or 2005 should be just fine. Below are a few QA style notes about installing FreeTDS.
NOTE: Windows users of our pre-compiled native gems need not worry about installing FreeTDS and its dependencies.
Do I need to install FreeTDS? Yes! Somehow, someway, you are going to need FreeTDS for TinyTDS to compile against.
OK, I am installing FreeTDS, how do I configure it? Contrary to what most people think, you do not need to specially configure FreeTDS in any way for client libraries like TinyTDS to use it. About the only requirement is that you compile it with libiconv for proper encoding support. FreeTDS must also be compiled with OpenSSL (or the like) to use it with Azure. See the "Using TinyTDS with Azure" section below for more info.
Do I need to configure --with-tdsver
equal to anything? Most likely! Technically you should not have to. This is only a default for clients/configs that do not specify what TDS version they want to use. We are currently having issues with passing down a TDS version with the login bit. Till we get that fixed, if you are not using a freetds.conf or a TDSVER environment variable, then make sure to use 7.1.
But I want to use TDS version 7.2 for SQL Server 2005 and up! TinyTDS uses TDS version 7.1 (previously named 8.0) and fully supports all the data types supported by FreeTDS, this includes varchar(max)
and nvarchar(max)
. Technically compiling and using TDS version 7.2 with FreeTDS is not supported. But this does not mean those data types will not work. I know, it's confusing If you want to learn more, read this thread. http://lists.ibiblio.org/pipermail/freetds/2011q3/027306.html
I want to configure FreeTDS using --enable-msdblib
and/or --enable-sybase-compat
so it works for my database. Cool? It's a waste of time and totally moot! Client libraries like TinyTDS define their own C structure names where they diverge from Sybase to SQL Server. Technically we use the MSDBLIB structures which does not mean we only work with that database vs Sybase. These configs are just a low level default for C libraries that do not define what they want. So I repeat, you do not NEED to use any of these, nor will they hurt anything since we control what C structure names we use internally!
Our goal is to support every SQL Server data type and covert it to a logical Ruby object. When dates or times are returned, they are instantiated to either :utc
or :local
time depending on the query options. Only [datetimeoffset] types are excluded. All strings are associated the to the connection's encoding and all binary data types are associated to Ruby's ASCII-8BIT/BINARY
encoding.
Below is a list of the data types we support when using the 7.3 TDS protocol version. Using a lower protocol version will result in these types being returned as strings.
Connect to a database.
client = TinyTds::Client.new username: 'sa', password: 'secret', host: 'mydb.host.net'
Creating a new client takes a hash of options. For valid iconv encoding options, see the output of iconv -l
. Only a few have been tested and highly recommended to leave blank for the UTF-8 default.
TinyTds::Client
object. If you are using 1.0rc5 or later, all clients will have an independent timeout setting as you'd expect. Timeouts caused by network failure will raise a timeout error 1 second after the configured timeout limit is hit (see #481 for details).call
-able object such as a Proc
or a method to receive info messages from the database. It should have a single parameter, which will be a TinyTds::Error
object representing the message. For example:opts = ... # host, username, password, etc
opts[:message_handler] = Proc.new { |m| puts m.message }
client = TinyTds::Client.new opts
# => Changed database context to 'master'.
# => Changed language setting to us_english.
client.execute("print 'hello world!'").do
# => hello world!
Use the #active?
method to determine if a connection is good. The implementation of this method may change but it should always guarantee that a connection is good. Current it checks for either a closed or dead connection.
client.dead? # => false
client.closed? # => false
client.active? # => true
client.execute("SQL TO A DEAD SERVER")
client.dead? # => true
client.closed? # => false
client.active? # => false
client.close
client.closed? # => true
client.active? # => false
Escape strings.
client.escape("How's It Going'") # => "How''s It Going''"
Send a SQL string to the database and return a TinyTds::Result object.
result = client.execute("SELECT * FROM [datatypes]")
A result object is returned by the client's execute command. It is important that you either return the data from the query, most likely with the #each method, or that you cancel the results before asking the client to execute another SQL batch. Failing to do so will yield an error.
Calling #each on the result will lazily load each row from the database.
result.each do |row|
# By default each row is a hash.
# The keys are the fields, as you'd expect.
# The values are pre-built Ruby primitives mapped from their corresponding types.
end
A result object has a #fields
accessor. It can be called before the result rows are iterated over. Even if no rows are returned, #fields will still return the column names you expected. Any SQL that does not return columned data will always return an empty array for #fields
. It is important to remember that if you access the #fields
before iterating over the results, the columns will always follow the default query option's :symbolize_keys
setting at the client's level and will ignore the query options passed to each.
result = client.execute("USE [tinytdstest]")
result.fields # => []
result.do
result = client.execute("SELECT [id] FROM [datatypes]")
result.fields # => ["id"]
result.cancel
result = client.execute("SELECT [id] FROM [datatypes]")
result.each(:symbolize_keys => true)
result.fields # => [:id]
You can cancel a result object's data from being loading by the server.
result = client.execute("SELECT * FROM [super_big_table]")
result.cancel
You can use results cancelation in conjunction with results lazy loading, no problem.
result = client.execute("SELECT * FROM [super_big_table]")
result.each_with_index do |row, i|
break if row > 10
end
result.cancel
If the SQL executed by the client returns affected rows, you can easily find out how many.
result.each
result.affected_rows # => 24
This pattern is so common for UPDATE and DELETE statements that the #do method cancels any need for loading the result data and returns the #affected_rows
.
result = client.execute("DELETE FROM [datatypes]")
result.do # => 72
Likewise for INSERT
statements, the #insert method cancels any need for loading the result data and executes a SCOPE_IDENTITY()
for the primary key.
result = client.execute("INSERT INTO [datatypes] ([xml]) VALUES ('<html><br/></html>')")
result.insert # => 420
The result object can handle multiple result sets form batched SQL or stored procedures. It is critical to remember that when calling each with a block for the first time will return each "row" of each result set. Calling each a second time with a block will yield each "set".
sql = ["SELECT TOP (1) [id] FROM [datatypes]",
"SELECT TOP (2) [bigint] FROM [datatypes] WHERE [bigint] IS NOT NULL"].join(' ')
set1, set2 = client.execute(sql).each
set1 # => [{"id"=>11}]
set2 # => [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
result = client.execute(sql)
result.each do |rowset|
# First time data loading, yields each row from each set.
# 1st: {"id"=>11}
# 2nd: {"bigint"=>-9223372036854775807}
# 3rd: {"bigint"=>9223372036854775806}
end
result.each do |rowset|
# Second time over (if columns cached), yields each set.
# 1st: [{"id"=>11}]
# 2nd: [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
end
Use the #sqlsent?
and #canceled?
query methods on the client to determine if an active SQL batch still needs to be processed and or if data results were canceled from the last result object. These values reset to true and false respectively for the client at the start of each #execute
and new result object. Or if all rows are processed normally, #sqlsent?
will return false. To demonstrate, lets assume we have 100 rows in the result object.
client.sqlsent? # = false
client.canceled? # = false
result = client.execute("SELECT * FROM [super_big_table]")
client.sqlsent? # = true
client.canceled? # = false
result.each do |row|
# Assume we break after 20 rows with 80 still pending.
break if row["id"] > 20
end
client.sqlsent? # = true
client.canceled? # = false
result.cancel
client.sqlsent? # = false
client.canceled? # = true
It is possible to get the return code after executing a stored procedure from either the result or client object.
client.return_code # => nil
result = client.execute("EXEC tinytds_TestReturnCodes")
result.do
result.return_code # => 420
client.return_code # => 420
Every TinyTds::Result
object can pass query options to the #each method. The defaults are defined and configurable by setting options in the TinyTds::Client.default_query_options
hash. The default values are:
Each result gets a copy of the default options you specify at the client level and can be overridden by passing an options hash to the #each method. For example
result.each(:as => :array, :cache_rows => false) do |row|
# Each row is now an array of values ordered by #fields.
# Rows are yielded and forgotten about, freeing memory.
end
Besides the standard query options, the result object can take one additional option. Using :first => true
will only load the first row of data and cancel all remaining results.
result = client.execute("SELECT * FROM [super_big_table]")
result.each(:first => true) # => [{'id' => 24}]
By default row caching is turned on because the SQL Server adapter for ActiveRecord would not work without it. I hope to find some time to create some performance patches for ActiveRecord that would allow it to take advantages of lazily created yielded rows from result objects. Currently only TinyTDS and the Mysql2 gem allow such a performance gain.
TinyTDS takes an opinionated stance on how we handle encoding errors. First, we treat errors differently on reads vs. writes. Our opinion is that if you are reading bad data due to your client's encoding option, you would rather just find ?
marks in your strings vs being blocked with exceptions. This is how things wold work via ODBC or SMS. On the other hand, writes will raise an exception. In this case we raise the SYBEICONVO/2402 error message which has a description of Error converting characters into server's character set. Some character(s) could not be converted.
. Even though the severity of this message is only a 4
and TinyTDS will automatically strip/ignore unknown characters, we feel you should know that you are inserting bad encodings. In this way, a transaction can be rolled back, etc. Remember, any database write that has bad characters due to the client encoding will still be written to the database, but it is up to you rollback said write if needed. Most ORMs like ActiveRecord handle this scenario just fine.
TinyTDS will raise a TinyTDS::Error
when a timeout is reached based on the options supplied to the client. Depending on the reason for the timeout, the connection could be dead or alive. When db processing is the cause for the timeout, the connection should still be usable after the error is raised. When network failure is the cause of the timeout, the connection will be dead. If you attempt to execute another command batch on a dead connection you will see a DBPROCESS is dead or not enabled
error. Therefore, it is recommended to check for a dead?
connection before trying to execute another command batch.
The TinyTDS gem uses binstub wrappers which mirror compiled FreeTDS Utilities binaries. These native executables are usually installed at the system level when installing FreeTDS. However, when using MiniPortile to install TinyTDS as we do with Windows binaries, these binstubs will find and prefer local gem exe
directory executables. These are the following binstubs we wrap.
TinyTDS is the default connection mode for the SQL Server adapter in versions 3.1 or higher. The SQL Server adapter can be found using the links below.
TinyTDS is fully tested with the Azure platform. You must set the azure: true
connection option when connecting. This is needed to specify the default database name in the login packet since Azure has no notion of USE [database]
. FreeTDS must be compiled with OpenSSL too.
IMPORTANT: Do not use username@server.database.windows.net
for the username connection option! You must use the shorter username@server
instead!
Also, please read the Azure SQL Database General Guidelines and Limitations MSDN article to understand the differences. Specifically, the connection constraints section!
A DBLIB connection does not have the same default SET options for a standard SMS SQL Server connection. Hence, we recommend the following options post establishing your connection.
SET ANSI_DEFAULTS ON
SET QUOTED_IDENTIFIER ON
SET CURSOR_CLOSE_ON_COMMIT OFF
SET IMPLICIT_TRANSACTIONS OFF
SET TEXTSIZE 2147483647
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_NULL_DFLT_ON ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET QUOTED_IDENTIFIER ON
SET CURSOR_CLOSE_ON_COMMIT OFF
SET IMPLICIT_TRANSACTIONS OFF
SET TEXTSIZE 2147483647
SET CONCAT_NULL_YIELDS_NULL ON
TinyTDS must be used with a connection pool for thread safety. If you use ActiveRecord or the Sequel gem this is done for you. However, if you are using TinyTDS on your own, we recommend using the ConnectionPool gem when using threads:
Please read our thread_test.rb file for details on how we test its usage.
This is possible using FreeTDS version 0.95 or higher. You must use the use_utf16
login option or add the following config to your freetds.conf
in either the global section or a specfic dataserver. If you are on Windows, the default location for your conf file will be in C:\Sites
.
[global]
use utf-16 = true
The default is true and since FreeTDS v1.0 would do this as well.
For the convenience of Windows users, TinyTDS ships pre-compiled gems for supported versions of Ruby on Windows. In order to generate these gems, rake-compiler-dock is used. This project provides several Docker images with rvm, cross-compilers and a number of different target versions of Ruby.
Run the following rake task to compile the gems for Windows. This will check the availability of Docker (and boot2docker on Windows or OS-X) and will give some advice for download and installation. When docker is running, it will download the docker image (once-only) and start the build:
$ rake gem:windows
The compiled gems will exist in ./pkg
directory.
First, clone the repo using the command line or your Git GUI of choice.
$ git clone git@github.com:rails-sqlserver/tiny_tds.git
After that, the quickest way to get setup for development is to use Docker. Assuming you have downloaded docker for your platform, you can use docker-compose to run the necessary containers for testing.
$ docker-compose up -d
This will download our SQL Server for Linux Docker image based from microsoft/mssql-server-linux/. Our image already has the [tinytdstest]
DB and tinytds
users created. This will also download a toxiproxy Docker image which we can use to simulate network failures for tests. Basically, it does the following.
$ docker network create main-network
$ docker pull metaskills/mssql-server-linux-tinytds
$ docker run -p 1433:1433 -d --name sqlserver --network main-network metaskills/mssql-server-linux-tinytds
$ docker pull shopify/toxiproxy
$ docker run -p 8474:8474 -p 1234:1234 -d --name toxiproxy --network main-network shopify/toxiproxy
If you are using your own database. Make sure to run these SQL commands as SA to get the test database and user installed.
CREATE DATABASE [tinytdstest];
CREATE LOGIN [tinytds] WITH PASSWORD = '', CHECK_POLICY = OFF, DEFAULT_DATABASE = [tinytdstest];
USE [tinytdstest];
CREATE USER [tinytds] FOR LOGIN [tinytds];
EXEC sp_addrolemember N'db_owner', N'tinytds';
From here you can build and run tests against an installed version of FreeTDS.
$ bundle install
$ bundle exec rake
Examples us using enviornment variables to customize the test task.
$ rake TINYTDS_UNIT_DATASERVER=mydbserver
$ rake TINYTDS_UNIT_DATASERVER=mydbserver TINYTDS_SCHEMA=sqlserver_2008
$ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_SCHEMA=sqlserver_azure
$ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_UNIT_PORT=5000 TINYTDS_SCHEMA=sybase_ase
If you use a multi stage Docker build to assemble your gems in one phase and then copy your app and gems into another, lighter, container without build tools you will need to make sure you tell the OS how to find dependencies for TinyTDS.
After you have built and installed FreeTDS it will normally place library files in /usr/local/lib
. When TinyTDS builds native extensions, it already knows to look here but if you copy your app to a new container that link will be broken.
Set the LD_LIBRARY_PATH environment variable export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}
and run ldconfig
. If you run ldd tiny_tds.so
you should not see any broken links. Make sure you also copied in the library dependencies from your build container with a command like COPY --from=builder /usr/local/lib /usr/local/lib
.
My name is Ken Collins and I currently maintain the SQL Server adapter for ActiveRecord and wrote this library as my first cut into learning Ruby C extensions. Hopefully it will help promote the power of Ruby and the Rails framework to those that have not yet discovered it. My blog is metaskills.net and I can be found on twitter as @metaskills. Enjoy!
TinyTDS is Copyright (c) 2010-2015 Ken Collins, ken@metaskills.net and Will Bond (Veracross LLC) wbond@breuer.com. It is distributed under the MIT license. Windows binaries contain pre-compiled versions of FreeTDS http://www.freetds.org/ which is licensed under the GNU LGPL license at http://www.gnu.org/licenses/lgpl-2.0.html
Author: rails-sqlserver
Source code: https://github.com/rails-sqlserver/tiny_tds
License:
1649392464
Flight rules for Git
A guide for astronauts (now, programmers using Git) about what to do when things go wrong.
Flight Rules are the hard-earned body of knowledge recorded in manuals that list, step-by-step, what to do if X occurs, and why. Essentially, they are extremely detailed, scenario-specific standard operating procedures. [...]
NASA has been capturing our missteps, disasters and solutions since the early 1960s, when Mercury-era ground teams first started gathering "lessons learned" into a compendium that now lists thousands of problematic situations, from engine failure to busted hatch handles to computer glitches, and their solutions.
— Chris Hadfield, An Astronaut's Guide to Life on Earth.
For clarity's sake all examples in this document use a customized bash prompt in order to indicate the current branch and whether or not there are staged changes. The branch is enclosed in parentheses, and a *
next to the branch name indicates staged changes.
All commands should work for at least git version 2.13.0. See the git website to update your local git version.
Table of Contents generated with DocToc
To initialize an existing directory as a Git repository:
(my-folder) $ git init
To clone (copy) a remote repository, copy the URL for the repository, and run:
$ git clone [url]
This will save it to a folder named the same as the remote repository's. Make sure you have a connection to the remote server you are cloning from (for most purposes this means making sure you are connected to the internet).
To clone it into a folder with a different name than the default repository name:
$ git clone [url] name-of-new-folder
There are a few possible problems here:
If you cloned the wrong repository, simply delete the directory created after running git clone
and clone the correct repository.
If you set the wrong repository as the origin of an existing local repository, change the URL of your origin by running:
$ git remote set-url origin [url of the actual repo]
For more, see this StackOverflow topic.
Git doesn't allow you to add code to someone else's repository without access rights. Neither does GitHub, which is not the same as Git, but rather a hosted service for Git repositories. However, you can suggest code using patches, or, on GitHub, forks and pull requests.
First, a bit about forking. A fork is a copy of a repository. It is not a git operation, but is a common action on GitHub, Bitbucket, GitLab — or anywhere people host Git repositories. You can fork a repository through the hosted UI.
After you've forked a repository, you normally need to clone the repository to your machine. You can do some small edits on GitHub, for instance, without cloning, but this isn't a github-flight-rules list, so let's go with how to do this locally.
# if you are using ssh
$ git clone git@github.com:k88hudson/git-flight-rules.git
# if you are using https
$ git clone https://github.com/k88hudson/git-flight-rules.git
If you cd
into the resulting directory, and type git remote
, you'll see a list of the remotes. Normally there will be one remote - origin
- which will point to k88hudson/git-flight-rules
. In this case, we also want a remote that will point to your fork.
First, to follow a Git convention, we normally use the remote name origin
for your own repository and upstream
for whatever you've forked. So, rename the origin
remote to upstream
$ git remote rename origin upstream
You can also do this using git remote set-url
, but it takes longer and is more steps.
Then, set up a new remote that points to your project.
$ git remote add origin git@github.com:YourName/git-flight-rules.git
Note that now you have two remotes.
origin
references your own repository.upstream
references the original one.From origin, you can read and write. From upstream, you can only read.
When you've finished making whatever changes you like, push your changes (normally in a branch) to the remote named origin
. If you're on a branch, you could use --set-upstream
to avoid specifying the remote tracking branch on every future push using this branch. For instance:
$ (feature/my-feature) git push --set-upstream origin feature/my-feature
There is no way to suggest a pull request using the CLI using Git (although there are tools, like hub, which will do this for you). So, if you're ready to make a pull request, go to your GitHub (or another Git host) and create a new pull request. Note that your host automatically links the original and forked repositories.
After all of this, do not forget to respond to any code review feedback.
Another approach to suggesting code changes that doesn't rely on third party sites such as Github is to use git format-patch
.
format-patch
creates a .patch file for one or more commits. This file is essentially a list of changes that looks similar to the commit diffs you can view on Github.
A patch can be viewed and even edited by the recipient and applied using git am
.
For example, to create a patch based on the previous commit you would run git format-patch HEAD^
which would create a .patch file called something like 0001-My-Commit-Message.patch.
To apply this patch file to your repository you would run git am ./0001-My-Commit-Message.patch
.
Patches can also be sent via email using the git send-email
command. For information on usage and configuration see: https://git-send-email.io
After a while, the upstream
repository may have been updated, and these updates need to be pulled into your origin
repo. Remember that like you, other people are contributing too. Suppose that you are in your own feature branch and you need to update it with the original repository updates.
You probably have set up a remote that points to the original project. If not, do this now. Generally we use upstream
as a remote name:
$ (main) git remote add upstream <link-to-original-repository>
# $ (main) git remote add upstream git@github.com:k88hudson/git-flight-rules.git
Now you can fetch from upstream and get the latest updates.
$ (main) git fetch upstream
$ (main) git merge upstream/main
# or using a single command
$ (main) git pull upstream main
Let's say that you just blindly committed changes with git commit -a
and you're not sure what the actual content of the commit you just made was. You can show the latest commit on your current HEAD with:
(main)$ git show
Or
$ git log -n1 -p
If you want to see a file at a specific commit, you can also do this (where <commitid>
is the commit you're interested in):
$ git show <commitid>:filename
If you wrote the wrong thing and the commit has not yet been pushed, you can do the following to change the commit message without changing the changes in the commit:
$ git commit --amend --only
This will open your default text editor, where you can edit the message. On the other hand, you can do this all in one command:
$ git commit --amend --only -m 'xxxxxxx'
If you have already pushed the message, you can amend the commit and force push, but this is not recommended.
If it's a single commit, amend it
$ git commit --amend --no-edit --author "New Authorname <authoremail@mydomain.com>"
An alternative is to correctly configure your author settings in git config --global author.(name|email)
and then use
$ git commit --amend --reset-author --no-edit
If you need to change all of history, see the man page for git filter-branch
.
In order to remove changes for a file from the previous commit, do the following:
$ git checkout HEAD^ myfile
$ git add myfile
$ git commit --amend --no-edit
In case the file was newly added to the commit and you want to remove it (from Git alone), do:
$ git rm --cached myfile
$ git commit --amend --no-edit
This is particularly useful when you have an open patch and you have committed an unnecessary file, and need to force push to update the patch on a remote. The --no-edit
option is used to keep the existing commit message.
If you need to delete pushed commits, you can use the following. However, it will irreversibly change your history, and mess up the history of anyone else who had already pulled from the repository. In short, if you're not sure, you should never do this, ever.
$ git reset HEAD^ --hard
$ git push --force-with-lease [remote] [branch]
If you haven't pushed, to reset Git to the state it was in before you made your last commit (while keeping your staged changes):
(my-branch*)$ git reset --soft HEAD@{1}
This only works if you haven't pushed. If you have pushed, the only truly safe thing to do is git revert SHAofBadCommit
. That will create a new commit that undoes all the previous commit's changes. Or, if the branch you pushed to is rebase-safe (ie. other devs aren't expected to pull from it), you can just use git push --force-with-lease
. For more, see the above section.
The same warning applies as above. Never do this if possible.
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push --force-with-lease [remote] [branch]
Or do an interactive rebase and remove the line(s) corresponding to commit(s) you want to see removed.
To https://github.com/yourusername/repo.git
! [rejected] mybranch -> mybranch (non-fast-forward)
error: failed to push some refs to 'https://github.com/tanay1337/webmaker.org.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Note that, as with rebasing (see below), amending replaces the old commit with a new one, so you must force push (--force-with-lease
) your changes if you have already pushed the pre-amended commit to your remote. Be careful when you do this – always make sure you specify a branch!
(my-branch)$ git push origin mybranch --force-with-lease
In general, avoid force pushing. It is best to create and push a new commit rather than force-pushing the amended commit as it will cause conflicts in the source history for any other developer who has interacted with the branch in question or any child branches. --force-with-lease
will still fail, if someone else was also working on the same branch as you, and your push would overwrite those changes.
If you are absolutely sure that nobody is working on the same branch or you want to update the tip of the branch unconditionally, you can use --force
(-f
), but this should be avoided in general.
If you accidentally do git reset --hard
, you can normally still get your commit back, as git keeps a log of everything for a few days.
Note: This is only valid if your work is backed up, i.e., either committed or stashed. git reset --hard
will remove uncommitted modifications, so use it with caution. (A safer option is git reset --keep
.)
(main)$ git reflog
You'll see a list of your past commits, and a commit for the reset. Choose the SHA of the commit you want to return to, and reset again:
(main)$ git reset --hard SHA1234
And you should be good to go.
If you accidentally merged a feature branch to the main development branch before it was ready to be merged, you can still undo the merge. But there's a catch: A merge commit has more than one parent (usually two).
The command to use
(feature-branch)$ git revert -m 1 <commit>
where the -m 1 option says to select parent number 1 (the branch into which the merge was made) as the parent to revert to.
Note: the parent number is not a commit identifier. Rather, a merge commit has a line Merge: 8e2ce2d 86ac2e7
. The parent number is the 1-based index of the desired parent on this line, the first identifier is number 1, the second is number 2, and so on.
If you accidentally pushed files containing sensitive, or private data (passwords, keys, etc.), you can amend the previous commit. Keep in mind that once you have pushed a commit, you should consider any data it contains to be compromised. These steps can remove the sensitive data from your public repo or your local copy, but you cannot remove the sensitive data from other people's pulled copies. If you committed a password, change it immediately. If you committed a key, re-generate it immediately. Amending the pushed commit is not enough, since anyone could have pulled the original commit containing your sensitive data in the meantime.
If you edit the file and remove the sensitive data, then run
(feature-branch)$ git add edited_file
(feature-branch)$ git commit --amend --no-edit
(feature-branch)$ git push --force-with-lease origin [branch]
If you want to remove an entire file (but keep it locally), then run
(feature-branch)$ git rm --cached sensitive_file
echo sensitive_file >> .gitignore
(feature-branch)$ git add .gitignore
(feature-branch)$ git commit --amend --no-edit
(feature-branch)$ git push --force-with-lease origin [branch]
Alternatively store your sensitive data in local environment variables.
If you want to completely remove an entire file (and not keep it locally), then run
(feature-branch)$ git rm sensitive_file
(feature-branch)$ git commit --amend --no-edit
(feature-branch)$ git push --force-with-lease origin [branch]
If you have made other commits in the meantime (i.e. the sensitive data is in a commit before the previous commit), you will have to rebase.
If the file you want to delete is secret or sensitive, instead see how to remove sensitive files.
Even if you delete a large or unwanted file in a recent commit, it still exists in git history, in your repo's .git
folder, and will make git clone
download unneeded files.
The actions in this part of the guide will require a force push, and rewrite large sections of repo history, so if you are working with remote collaborators, check first that any local work of theirs is pushed.
There are two options for rewriting history, the built-in git-filter-branch
or bfg-repo-cleaner
. bfg
is significantly cleaner and more performant, but it is a third-party download and requires java. We will describe both alternatives. The final step is to force push your changes, which requires special consideration on top of a regular force push, given that a great deal of repo history will have been permanently changed.
Using bfg-repo-cleaner requires java. Download the bfg jar from the link here. Our examples will use bfg.jar
, but your download may have a version number, e.g. bfg-1.13.0.jar
.
To delete a specific file.
(main)$ git rm path/to/filetoremove
(main)$ git commit -m "Commit removing filetoremove"
(main)$ java -jar ~/Downloads/bfg.jar --delete-files filetoremove
Note that in bfg you must use the plain file name even if it is in a subdirectory.
You can also delete a file by pattern, e.g.:
(main)$ git rm *.jpg
(main)$ git commit -m "Commit removing *.jpg"
(main)$ java -jar ~/Downloads/bfg.jar --delete-files *.jpg
With bfg, the files that exist on your latest commit will not be affected. For example, if you had several large .tga files in your repo, and then in an earlier commit, you deleted a subset of them, this call does not touch files present in the latest commit
Note, if you renamed a file as part of a commit, e.g. if it started as LargeFileFirstName.mp4
and a commit changed it to LargeFileSecondName.mp4
, running java -jar ~/Downloads/bfg.jar --delete-files LargeFileSecondName.mp4
will not remove it from git history. Either run the --delete-files
command with both filenames, or with a matching pattern.
git-filter-branch
is more cumbersome and has less features, but you may use it if you cannot install or run bfg
.
In the below, replace filepattern
may be a specific name or pattern, e.g. *.jpg
. This will remove files matching the pattern from all history and branches.
(main)$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch filepattern' --prune-empty --tag-name-filter cat -- --all
Behind-the-scenes explanation:
--tag-name-filter cat
is a cumbersome, but simplest, way to apply the original tags to the new commits, using the command cat.
--prune-empty
removes any now-empty commits.
Once you have removed your desired files, test carefully that you haven't broken anything in your repo - if you have, it is easiest to re-clone your repo to start over. To finish, optionally use git garbage collection to minimize your local .git folder size, and then force push.
(main)$ git reflog expire --expire=now --all && git gc --prune=now --aggressive
(main)$ git push origin --force --tags
Since you just rewrote the entire git repo history, the git push
operation may be too large, and return the error “The remote end hung up unexpectedly”
. If this happens, you can try increasing the git post buffer:
(main)$ git config http.postBuffer 524288000
(main)$ git push --force
If this does not work, you will need to manually push the repo history in chunks of commits. In the command below, try increasing <number>
until the push operation succeeds.
(main)$ git push -u origin HEAD~<number>:refs/head/main --force
Once the push operation succeeds the first time, decrease <number>
gradually until a conventional git push
succeeds.
Consider you created some (e.g. three) commits and later realize you missed doing something that belongs contextually into the first of those commits. This bothers you, because if you'd create a new commit containing those changes, you'd have a clean code base, but your commits weren't atomic (i.e. changes that belonged to each other weren't in the same commit). In such a situation you may want to change the commit where these changes belong to, include them and have the following commits unaltered. In such a case, git rebase
might save you.
Consider a situation where you want to change the third last commit you made.
(your-branch)$ git rebase -i HEAD~4
gets you into interactive rebase mode, which allows you to edit any of your last three commits. A text editor pops up, showing you something like
pick 9e1d264 The third last commit
pick 4b6e19a The second to last commit
pick f4037ec The last commit
which you change into
edit 9e1d264 The third last commit
pick 4b6e19a The second to last commit
pick f4037ec The last commit
This tells rebase that you want to edit your third last commit and keep the other two unaltered. Then you'll save (and close) the editor. Git will then start to rebase. It stops on the commit you want to alter, giving you the chance to edit that commit. Now you can apply the changes which you missed applying when you initially committed that commit. You do so by editing and staging them. Afterwards you'll run
(your-branch)$ git commit --amend
which tells Git to recreate the commit, but to leave the commit message unedited. Having done that, the hard part is solved.
(your-branch)$ git rebase --continue
will do the rest of the work for you.
$ git add -u
# to stage files with ext .txt
$ git add -u *.txt
# to stage all files inside directory src
$ git add -u src/
(my-branch*)$ git commit --amend
If you already know you don't want to change the commit message, you can tell git to reuse the commit message:
(my-branch*)$ git commit --amend -C HEAD
Normally, if you want to stage part of a file, you run this:
$ git add --patch filename.x
-p
will work for short. This will open interactive mode. You would be able to use the s
option to split the commit - however, if the file is new, you will not have this option. To add a new file, do this:
$ git add -N filename.x
Then, you will need to use the e
option to manually choose which lines to add. Running git diff --cached
or git diff --staged
will show you which lines you have staged compared to which are still saved locally.
git add
will add the entire file to a commit. git add -p
will allow to interactively select which changes you want to add.
git reset -p
will open a patch mode reset dialog. This is similar to git add -p
, except that selecting "yes" will unstage the change, removing it from the upcoming commit.
In many cases, you should unstage all of your staged files and then pick the file you want and commit it. However, if you want to switch the staged and unstaged edits, you can create a temporary commit to store your staged files, stage your unstaged files and then stash them. Then, reset the temporary commit and pop your stash.
$ git commit -m "WIP"
$ git add . # This will also add untracked files.
$ git stash
$ git reset HEAD^
$ git stash pop --index 0
NOTE 1: The reason to use pop
here is want to keep idempotent as much as possible. NOTE 2: Your staged files will be marked as unstaged if you don't use the --index
flag. (This link explains why.)
$ git checkout -b my-branch
$ git stash
$ git checkout my-branch
$ git stash pop
If you want to discard all your local staged and unstaged changes, you can do this:
(my-branch)$ git reset --hard
# or
(main)$ git checkout -f
This will unstage all files you might have staged with git add
:
$ git reset
This will revert all local uncommitted changes (should be executed in repo root):
$ git checkout .
You can also revert uncommitted changes to a particular file or directory:
$ git checkout [some_dir|file.txt]
Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):
$ git reset --hard HEAD
This will remove all local untracked files, so only files tracked by Git remain:
$ git clean -fd
-x
will also remove all ignored files.
When you want to get rid of some, but not all changes in your working copy.
Checkout undesired changes, keep good changes.
$ git checkout -p
# Answer y to all of the snippets you want to drop
Another strategy involves using stash
. Stash all the good changes, reset working copy, and reapply good changes.
$ git stash -p
# Select all of the snippets you want to save
$ git reset --hard
$ git stash pop
Alternatively, stash your undesired changes, and then drop stash.
$ git stash -p
# Select all of the snippets you don't want to save
$ git stash drop
When you want to get rid of one specific file in your working copy.
$ git checkout myFile
Alternatively, to discard multiple files in your working copy, list them all.
$ git checkout myFirstFile mySecondFile
When you want to get rid of all of your unstaged local uncommitted changes
$ git checkout .
When you want to get rid of all of your untracked files
$ git clean -f
Sometimes we have one or more files that accidentally ended up being staged, and these files have not been committed before. To unstage them:
$ git reset -- <filename>
This results in unstaging the file and make it look like it's untracked.
List local branches
$ git branch
List remote branches
$ git branch -r
List all branches (both local and remote)
$ git branch -a
$ git checkout -b <branch> <SHA1_OF_COMMIT>
This is another chance to use git reflog
to see where your HEAD pointed before the bad pull.
(main)$ git reflog
ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward
c5bc55a HEAD@{1}: checkout: checkout message goes here
Simply reset your branch back to the desired commit:
$ git reset --hard c5bc55a
Done.
Confirm that you haven't pushed your changes to the server.
git status
should show how many commits you are ahead of origin:
(my-branch)$ git status
# On branch my-branch
# Your branch is ahead of 'origin/my-branch' by 2 commits.
# (use "git push" to publish your local commits)
#
One way of resetting to match origin (to have the same as what is on the remote) is to do this:
(main)$ git reset --hard origin/my-branch
Create the new branch while remaining on main:
(main)$ git branch my-branch
Reset the branch main to the previous commit:
(main)$ git reset --hard HEAD^
HEAD^
is short for HEAD^1
. This stands for the first parent of HEAD
, similarly HEAD^2
stands for the second parent of the commit (merges can have 2 parents).
Note that HEAD^2
is not the same as HEAD~2
(see this link for more information).
Alternatively, if you don't want to use HEAD^
, find out what the commit hash you want to set your main branch to (git log
should do the trick). Then reset to that hash. git push
will make sure that this change is reflected on your remote.
For example, if the hash of the commit that your main branch is supposed to be at is a13b85e
:
(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
Checkout the new branch to continue working:
(main)$ git checkout my-branch
Say you have a working spike (see note), with hundreds of changes. Everything is working. Now, you commit into another branch to save that work:
(solution)$ git add -A && git commit -m "Adding all changes from this spike into one big commit."
When you want to put it into a branch (maybe feature, maybe develop
), you're interested in keeping whole files. You want to split your big commit into smaller ones.
Say you have:
solution
, with the solution to your spike. One ahead of develop
.develop
, where you want to add your changes.You can solve it bringing the contents to your branch:
(develop)$ git checkout solution -- file1.txt
This will get the contents of that file in branch solution
to your branch develop
:
# On branch develop
# Your branch is up-to-date with 'origin/develop'.
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file1.txt
Then, commit as usual.
Note: Spike solutions are made to analyze or solve the problem. These solutions are used for estimation and discarded once everyone gets clear visualization of the problem. ~ Wikipedia.
Say you are on your main branch. Running git log
, you see you have made two commits:
(main)$ git log
commit e3851e817c451cc36f2e6f3049db528415e3c114
Author: Alex Lee <alexlee@example.com>
Date: Tue Jul 22 15:39:27 2014 -0400
Bug #21 - Added CSRF protection
commit 5ea51731d150f7ddc4a365437931cd8be3bf3131
Author: Alex Lee <alexlee@example.com>
Date: Tue Jul 22 15:39:12 2014 -0400
Bug #14 - Fixed spacing on title
commit a13b85e984171c6e2a1729bb061994525f626d14
Author: Aki Rose <akirose@example.com>
Date: Tue Jul 21 01:12:48 2014 -0400
First commit
Let's take note of our commit hashes for each bug (e3851e8
for #21, 5ea5173
for #14).
First, let's reset our main branch to the correct commit (a13b85e
):
(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
Now, we can create a fresh branch for our bug #21:
(main)$ git checkout -b 21
(21)$
Now, let's cherry-pick the commit for bug #21 on top of our branch. That means we will be applying that commit, and only that commit, directly on top of whatever our head is at.
(21)$ git cherry-pick e3851e8
At this point, there is a possibility there might be conflicts. See the There were conflicts section in the interactive rebasing section above for how to resolve conflicts.
Now let's create a new branch for bug #14, also based on main
(21)$ git checkout main
(main)$ git checkout -b 14
(14)$
And finally, let's cherry-pick the commit for bug #14:
(14)$ git cherry-pick 5ea5173
Once you merge a pull request on GitHub, it gives you the option to delete the merged branch in your fork. If you aren't planning to keep working on the branch, it's cleaner to delete the local copies of the branch so you don't end up cluttering up your working checkout with a lot of stale branches.
$ git fetch -p upstream
where, upstream
is the remote you want to fetch from.
If you're regularly pushing to remote, you should be safe most of the time. But still sometimes you may end up deleting your branches. Let's say we create a branch and create a new file:
(main)$ git checkout -b my-branch
(my-branch)$ git branch
(my-branch)$ touch foo.txt
(my-branch)$ ls
README.md foo.txt
Let's add it and commit.
(my-branch)$ git add .
(my-branch)$ git commit -m 'foo.txt added'
(my-branch)$ foo.txt added
1 files changed, 1 insertions(+)
create mode 100644 foo.txt
(my-branch)$ git log
commit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012
Author: siemiatj <siemiatj@example.com>
Date: Wed Jul 30 00:34:10 2014 +0200
foo.txt added
commit 69204cdf0acbab201619d95ad8295928e7f411d5
Author: Kate Hudson <katehudson@example.com>
Date: Tue Jul 29 13:14:46 2014 -0400
Fixes #6: Force pushing after amending commits
Now we're switching back to main and 'accidentally' removing our branch.
(my-branch)$ git checkout main
Switched to branch 'main'
Your branch is up-to-date with 'origin/main'.
(main)$ git branch -D my-branch
Deleted branch my-branch (was 4e3cd85).
(main)$ echo oh noes, deleted my branch!
oh noes, deleted my branch!
At this point you should get familiar with 'reflog', an upgraded logger. It stores the history of all the action in the repo.
(main)$ git reflog
69204cd HEAD@{0}: checkout: moving from my-branch to main
4e3cd85 HEAD@{1}: commit: foo.txt added
69204cd HEAD@{2}: checkout: moving from main to my-branch
As you can see we have commit hash from our deleted branch. Let's see if we can restore our deleted branch.
(main)$ git checkout -b my-branch-help
Switched to a new branch 'my-branch-help'
(my-branch-help)$ git reset --hard 4e3cd85
HEAD is now at 4e3cd85 foo.txt added
(my-branch-help)$ ls
README.md foo.txt
Voila! We got our removed file back. git reflog
is also useful when rebasing goes terribly wrong.
To delete a remote branch:
(main)$ git push origin --delete my-branch
You can also do:
(main)$ git push origin :my-branch
To delete a local branch:
(main)$ git branch -d my-branch
To delete a local branch that has not been merged to the current branch or an upstream:
(main)$ git branch -D my-branch
Say you want to delete all branches that start with fix/
:
(main)$ git branch | grep 'fix/' | xargs git branch -d
To rename the current (local) branch:
(main)$ git branch -m new-name
To rename a different (local) branch:
(main)$ git branch -m old-name new-name
To delete the old-name
remote branch and push the new-name
local branch:
(main)$ git push origin :old_name new_name
First, fetch all branches from remote:
(main)$ git fetch --all
Say you want to checkout to daves
from the remote.
(main)$ git checkout --track origin/daves
Branch daves set up to track remote branch daves from origin.
Switched to a new branch 'daves'
(--track
is shorthand for git checkout -b [branch] [remotename]/[branch]
)
This will give you a local copy of the branch daves
, and any update that has been pushed will also show up remotely.
$ git push <remote> HEAD
If you would also like to set that remote branch as upstream for the current one, use the following instead:
$ git push -u <remote> HEAD
With the upstream
mode and the simple
(default in Git 2.0) mode of the push.default
config, the following command will push the current branch with regards to the remote branch that has been registered previously with -u
:
$ git push
The behavior of the other modes of git push
is described in the doc of push.default
.
You can set a remote branch as the upstream for the current local branch using:
$ git branch --set-upstream-to [remotename]/[branch]
# or, using the shorthand:
$ git branch -u [remotename]/[branch]
To set the upstream remote branch for another local branch:
$ git branch -u [remotename]/[branch] [local-branch]
By checking your remote branches, you can see which remote branch your HEAD is tracking. In some cases, this is not the desired branch.
$ git branch -r
origin/HEAD -> origin/gh-pages
origin/main
To change origin/HEAD
to track origin/main
, you can run this command:
$ git remote set-head origin --auto
origin/HEAD set to main
You've made uncommitted changes and realise you're on the wrong branch. Stash changes and apply them to the branch you want:
(wrong_branch)$ git stash
(wrong_branch)$ git checkout <correct_branch>
(correct_branch)$ git stash apply
You've made a lot of commits on a branch and now want to separate it into two, ending with a branch up to an earlier commit and another with all the changes.
Use git log
to find the commit where you want to split. Then do the following:
(original_branch)$ git checkout -b new_branch
(new_branch)$ git checkout original_branch
(original_branch)$ git reset --hard <sha1 split here>
If you had previously pushed the original_branch
to remote, you will need to do a force push. For more information check Stack Overlflow
You may have merged or rebased your current branch with a wrong branch, or you can't figure it out or finish the rebase/merge process. Git saves the original HEAD pointer in a variable called ORIG_HEAD before doing dangerous operations, so it is simple to recover your branch at the state before the rebase/merge.
(my-branch)$ git reset --hard ORIG_HEAD
Unfortunately, you have to force push, if you want those changes to be reflected on the remote branch. This is because you have changed the history. The remote branch won't accept changes unless you force push. This is one of the main reasons many people use a merge workflow, instead of a rebasing workflow - large teams can get into trouble with developers force pushing. Use this with caution. A safer way to use rebase is not to reflect your changes on the remote branch at all, and instead to do the following:
(main)$ git checkout my-branch
(my-branch)$ git rebase -i main
(my-branch)$ git checkout main
(main)$ git merge --ff-only my-branch
For more, see this SO thread.
Let's suppose you are working in a branch that is/will become a pull-request against main
. In the simplest case when all you want to do is to combine all commits into a single one and you don't care about commit timestamps, you can reset and recommit. Make sure the main branch is up to date and all your changes committed, then:
(my-branch)$ git reset --soft main
(my-branch)$ git commit -am "New awesome feature"
If you want more control, and also to preserve timestamps, you need to do something called an interactive rebase:
(my-branch)$ git rebase -i main
If you aren't working against another branch you'll have to rebase relative to your HEAD
. If you want to squash the last 2 commits, for example, you'll have to rebase against HEAD~2
. For the last 3, HEAD~3
, etc.
(main)$ git rebase -i HEAD~2
After you run the interactive rebase command, you will see something like this in your text editor:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
pick b729ad5 fixup
pick e3851e8 another fix
# Rebase 8074d12..b729ad5 onto 8074d12
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
All the lines beginning with a #
are comments, they won't affect your rebase.
Then you replace pick
commands with any in the list above, and you can also remove commits by removing corresponding lines.
For example, if you want to leave the oldest (first) commit alone and combine all the following commits with the second oldest, you should edit the letter next to each commit except the first and the second to say f
:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
f b729ad5 fixup
f e3851e8 another fix
If you want to combine these commits and rename the commit, you should additionally add an r
next to the second commit or simply use s
instead of f
:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
s b729ad5 fixup
s e3851e8 another fix
You can then rename the commit in the next text prompt that pops up.
Newer, awesomer features
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 8074d12
# You are currently editing a commit while rebasing branch 'main' on '8074d12'.
#
# Changes to be committed:
# modified: README.md
#
If everything is successful, you should see something like this:
(main)$ Successfully rebased and updated refs/heads/main.
--no-commit
performs the merge but pretends the merge failed and does not autocommit, giving the user a chance to inspect and further tweak the merge result before committing. no-ff
maintains evidence that a feature branch once existed, keeping project history consistent.
(main)$ git merge --no-ff --no-commit my-branch
(main)$ git merge --squash my-branch
Sometimes you have several work in progress commits that you want to combine before you push them upstream. You don't want to accidentally combine any commits that have already been pushed upstream because someone else may have already made commits that reference them.
(main)$ git rebase -i @{u}
This will do an interactive rebase that lists only the commits that you haven't already pushed, so it will be safe to reorder/fix/squash anything in the list.
Sometimes the merge can produce problems in certain files, in those cases we can use the option abort
to abort the current conflict resolution process, and try to reconstruct the pre-merge state.
(my-branch)$ git merge --abort
This command is available since Git version >= 1.7.4
Say I have a main branch, a feature-1 branch branched from main, and a feature-2 branch branched off of feature-1. If I make a commit to feature-1, then the parent commit of feature-2 is no longer accurate (it should be the head of feature-1, since we branched off of it). We can fix this with git rebase --onto
.
(feature-2)$ git rebase --onto feature-1 <the first commit in your feature-2 branch that you don't want to bring along> feature-2
This helps in sticky scenarios where you might have a feature built on another feature that hasn't been merged yet, and a bugfix on the feature-1 branch needs to be reflected in your feature-2 branch.
To check if all commits on a branch are merged into another branch, you should diff between the heads (or any commits) of those branches:
(main)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll
This will tell you if any commits are in one but not the other, and will give you a list of any nonshared between the branches. Another option is to do this:
(main)$ git log main ^feature/120-on-scroll --no-merges
If you're seeing this:
noop
That means you are trying to rebase against a branch that is at an identical commit, or is ahead of your current branch. You can try:
HEAD~2
or earlier instead
If you are unable to successfully complete the rebase, you may have to resolve conflicts.
First run git status
to see which files have conflicts in them:
(my-branch)$ git status
On branch my-branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
both modified: README.md
In this example, README.md
has conflicts. Open that file and look for the following:
<<<<<<< HEAD
some code
=========
some code
>>>>>>> new-commit
You will need to resolve the differences between the code that was added in your new commit (in the example, everything from the middle line to new-commit
) and your HEAD
.
If you want to keep one branch's version of the code, you can use --ours
or --theirs
:
(main*)$ git checkout --ours README.md
--ours
to keep changes from the local branch, or --theirs
to keep changes from the other branch.--theirs
to keep changes from the local branch, or --ours
to keep changes from the other branch. For an explanation of this swap, see this note in the Git documentation.If the merges are more complicated, you can use a visual diff editor:
(main*)$ git mergetool -t opendiff
After you have resolved all conflicts and tested your code, git add
the files you have changed, and then continue the rebase with git rebase --continue
(my-branch)$ git add README.md
(my-branch)$ git rebase --continue
If after resolving all the conflicts you end up with an identical tree to what it was before the commit, you need to git rebase --skip
instead.
If at any time you want to stop the entire rebase and go back to the original state of your branch, you can do so:
(my-branch)$ git rebase --abort
To stash all the edits in your working directory
$ git stash
If you also want to stash untracked files, use -u
option.
$ git stash -u
To stash only one file from your working directory
$ git stash push working-directory-path/filename.ext
To stash multiple files from your working directory
$ git stash push working-directory-path/filename1.ext working-directory-path/filename2.ext
$ git stash save <message>
or
$ git stash push -m <message>
First check your list of stashes with message using
$ git stash list
Then apply a specific stash from the list using
$ git stash apply "stash@{n}"
Here, 'n' indicates the position of the stash in the stack. The topmost stash will be position 0.
Furthermore, using a time-based stash reference is also possible.
$ git stash apply "stash@{2.hours.ago}"
You can manually create a stash commit
, and then use git stash store
.
$ git stash create
$ git stash store -m <message> CREATED_SHA1
To find a certain string which was introduced in any commit, you can use the following structure:
$ git log -S "string to find"
Commons parameters:
--source
means to show the ref name given on the command line by which each commit was reached.
--all
means to start from every branch.
--reverse
prints in reverse order, it means that will show the first commit that made the change.
To find all commits by author/committer you can use:
$ git log --author=<name or email>
$ git log --committer=<name or email>
Keep in mind that author and committer are not the same. The --author
is the person who originally wrote the code; on the other hand, the --committer
, is the person who committed the code on behalf of the original author.
To find all commits containing a specific file you can use:
$ git log -- <path to file>
You would usually specify an exact path, but you may also use wild cards in the path and file name:
$ git log -- **/*.js
While using wildcards, it's useful to inform --name-status
to see the list of committed files:
$ git log --name-status -- **/*.js
To trace the evolution of a single function you can use:
$ git log -L :FunctionName:FilePath
Note that you can combine this with further git log
options, like revision ranges and commit limits.
To find all tags containing a specific commit:
$ git tag --contains <commitid>
$ git clone --recursive git://github.com/foo/bar.git
If already cloned:
$ git submodule update --init --recursive
Creating a submodule is pretty straight-forward, but deleting them less so. The commands you need are:
$ git submodule deinit submodulename
$ git rm submodulename
$ git rm --cached submodulename
$ rm -rf .git/modules/submodulename
$ git checkout <branch-you-want-the-directory-from> -- <folder-name or file-name>
First find the commit when the file last existed:
$ git rev-list -n 1 HEAD -- filename
Then checkout that file:
git checkout deletingcommitid^ -- filename
$ git tag -d <tag_name>
$ git push <remote> :refs/tags/<tag_name>
If you want to recover a tag that was already deleted, you can do so by following these steps: First, you need to find the unreachable tag:
$ git fsck --unreachable | grep tag
Make a note of the tag's hash. Then, restore the deleted tag with following, making use of git update-ref
:
$ git update-ref refs/tags/<tag_name> <hash>
Your tag should now have been restored.
If someone has sent you a pull request on GitHub, but then deleted their original fork, you will be unable to clone their repository or to use git am
as the .diff, .patch URLs become unavailable. But you can checkout the PR itself using GitHub's special refs. To fetch the content of PR#1 into a new branch called pr_1:
$ git fetch origin refs/pull/1/head:pr_1
From github.com:foo/bar
* [new ref] refs/pull/1/head -> pr_1
$ git archive --format zip --output /full/path/to/zipfile.zip main
If there is a tag on a remote repository that has the same name as a branch you will get the following error when trying to push that branch with a standard $ git push <remote> <branch>
command.
$ git push origin <branch>
error: dst refspec same matches more than one.
error: failed to push some refs to '<git server>'
Fix this by specifying you want to push the head reference.
$ git push origin refs/heads/<branch-name>
If you want to push a tag to a remote repository that has the same name as a branch, you can use a similar command.
$ git push origin refs/tags/<tag-name>
(main)$ git mv --force myfile MyFile
(main)$ git fetch --all
(main)$ git reset --hard origin/main
(main)$ git rm --cached log.txt
Assuming the hash of the commit you want is c5f567:
(main)$ git checkout c5f567 -- file1/to/restore file2/to/restore
If you want to revert to changes made just 1 commit before c5f567, pass the commit hash as c5f567~1:
(main)$ git checkout c5f567~1 -- file1/to/restore file2/to/restore
Assuming you want to compare last commit with file from commit c5f567:
$ git diff HEAD:path_to_file/file c5f567:path_to_file/file
Same goes for branches:
$ git diff main:path_to_file/file staging:path_to_file/file
This works great for config templates or other files that require locally adding credentials that shouldn't be committed.
$ git update-index --assume-unchanged file-to-ignore
Note that this does not remove the file from source control - it is only ignored locally. To undo this and tell Git to notice changes again, this clears the ignore flag:
$ git update-index --no-assume-unchanged file-to-stop-ignoring
The git-bisect command uses a binary search to find which commit in your Git history introduced a bug.
Suppose you're on the main
branch, and you want to find the commit that broke some feature. You start bisect:
$ git bisect start
Then you should specify which commit is bad, and which one is known to be good. Assuming that your current version is bad, and v1.1.1
is good:
$ git bisect bad
$ git bisect good v1.1.1
Now git-bisect
selects a commit in the middle of the range that you specified, checks it out, and asks you whether it's good or bad. You should see something like:
$ Bisecting: 5 revision left to test after this (roughly 5 step)
$ [c44abbbee29cb93d8499283101fe7c8d9d97f0fe] Commit message
$ (c44abbb)$
You will now check if this commit is good or bad. If it's good:
$ (c44abbb)$ git bisect good
and git-bisect
will select another commit from the range for you. This process (selecting good
or bad
) will repeat until there are no more revisions left to inspect, and the command will finally print a description of the first bad commit.
On OS X and Linux, your git configuration file is stored in ~/.gitconfig
. I've added some example aliases I use as shortcuts (and some of my common typos) in the [alias]
section as shown below:
[alias]
a = add
amend = commit --amend
c = commit
ca = commit --amend
ci = commit -a
co = checkout
d = diff
dc = diff --changed
ds = diff --staged
extend = commit --amend -C HEAD
f = fetch
loll = log --graph --decorate --pretty=oneline --abbrev-commit
m = merge
one = log --pretty=oneline
outstanding = rebase -i @{u}
reword = commit --amend --only
s = status
unpushed = log @{u}
wc = whatchanged
wip = rebase -i @{u}
zap = fetch -p
day = log --reverse --no-merges --branches=* --date=local --since=midnight --author=\"$(git config --get user.name)\"
delete-merged-branches = "!f() { git checkout --quiet main && git branch --merged | grep --invert-match '\\*' | xargs -n 1 git branch --delete; git checkout --quiet @{-1}; }; f"
You can’t! Git doesn’t support this, but there’s a hack. You can create a .gitignore file in the directory with the following contents:
# Ignore everything in this directory
*
# Except this file
!.gitignore
Another common convention is to make an empty file in the folder, titled .gitkeep.
$ mkdir mydir
$ touch mydir/.gitkeep
You can also name the file as just .keep , in which case the second line above would be touch mydir/.keep
You might have a repository that requires authentication. In which case you can cache a username and password so you don't have to enter it on every push and pull. Credential helper can do this for you.
$ git config --global credential.helper cache
# Set git to use the credential memory cache
$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)
To find a credential helper:
$ git help -a | grep credential
# Shows you possible credential helpers
For OS specific credential caching:
$ git config --global credential.helper osxkeychain
# For OSX
$ git config --global credential.helper manager
# Git for Windows 2.7.3+
$ git config --global credential.helper gnome-keyring
# Ubuntu and other GNOME-based distros
More credential helpers can likely be found for different distributions and operating systems.
$ git config core.fileMode false
If you want to make this the default behaviour for logged-in users, then use:
$ git config --global core.fileMode false
To configure user information used across all local repositories, and to set a name that is identifiable for credit when review version history:
$ git config --global user.name “[firstname lastname]”
To set an email address that will be associated with each history marker:
git config --global user.email “[valid-email]”
So, you're screwed - you reset
something, or you merged the wrong branch, or you force pushed and now you can't find your commits. You know, at some point, you were doing alright, and you want to go back to some state you were at.
This is what git reflog
is for. reflog
keeps track of any changes to the tip of a branch, even if that tip isn't referenced by a branch or a tag. Basically, every time HEAD changes, a new entry is added to the reflog. This only works for local repositories, sadly, and it only tracks movements (not changes to a file that weren't recorded anywhere, for instance).
(main)$ git reflog
0a2e358 HEAD@{0}: reset: moving to HEAD~2
0254ea7 HEAD@{1}: checkout: moving from 2.2 to main
c10f740 HEAD@{2}: checkout: moving from main to 2.2
The reflog above shows a checkout from main to the 2.2 branch and back. From there, there's a hard reset to an older commit. The latest activity is represented at the top labeled HEAD@{0}
.
If it turns out that you accidentally moved back, the reflog will contain the commit main pointed to (0254ea7) before you accidentally dropped 2 commits.
$ git reset --hard 0254ea7
Using git reset
it is then possible to change main back to the commit it was before. This provides a safety net in case history was accidentally changed.
(copied and edited from Source).
Once you're comfortable with what the above commands are doing, you might want to create some shortcuts for Git Bash. This allows you to work a lot faster by doing complex tasks in really short commands.
alias sq=squash
function squash() {
git rebase -i HEAD~$1
}
Copy those commands to your .bashrc or .bash_profile.
If you are using PowerShell on Windows, you can also set up aliases and functions. Add these commands to your profile, whose path is defined in the $profile
variable. Learn more at the About Profiles page on the Microsoft documentation site.
Set-Alias sq Squash-Commits
function Squash-Commits {
git rebase -i HEAD~$1
}
Other Resources
🌍 English ∙ Español ∙ Русский ∙ 简体中文∙ 한국어 ∙ Tiếng Việt ∙ Français ∙ 日本語
Author: K88hudson
Source Code: https://github.com/k88hudson/git-flight-rules
License: CC-BY-SA-4.0 License
1642173480
Flight rules for Git
🌍 English ∙ Español ∙ Русский ∙ 简体中文∙ 한국어 ∙ Tiếng Việt ∙ Français ∙ 日本語
A guide for astronauts (now, programmers using Git) about what to do when things go wrong.
Flight Rules are the hard-earned body of knowledge recorded in manuals that list, step-by-step, what to do if X occurs, and why. Essentially, they are extremely detailed, scenario-specific standard operating procedures. [...]
NASA has been capturing our missteps, disasters and solutions since the early 1960s, when Mercury-era ground teams first started gathering "lessons learned" into a compendium that now lists thousands of problematic situations, from engine failure to busted hatch handles to computer glitches, and their solutions.
— Chris Hadfield, An Astronaut's Guide to Life on Earth.
For clarity's sake all examples in this document use a customized bash prompt in order to indicate the current branch and whether or not there are staged changes. The branch is enclosed in parentheses, and a *
next to the branch name indicates staged changes.
All commands should work for at least git version 2.13.0. See the git website to update your local git version.
Table of Contents generated with DocToc
To initialize an existing directory as a Git repository:
(my-folder) $ git init
To clone (copy) a remote repository, copy the URL for the repository, and run:
$ git clone [url]
This will save it to a folder named the same as the remote repository's. Make sure you have a connection to the remote server you are cloning from (for most purposes this means making sure you are connected to the internet).
To clone it into a folder with a different name than the default repository name:
$ git clone [url] name-of-new-folder
There are a few possible problems here:
If you cloned the wrong repository, simply delete the directory created after running git clone
and clone the correct repository.
If you set the wrong repository as the origin of an existing local repository, change the URL of your origin by running:
$ git remote set-url origin [url of the actual repo]
For more, see this StackOverflow topic.
Git doesn't allow you to add code to someone else's repository without access rights. Neither does GitHub, which is not the same as Git, but rather a hosted service for Git repositories. However, you can suggest code using patches, or, on GitHub, forks and pull requests.
First, a bit about forking. A fork is a copy of a repository. It is not a git operation, but is a common action on GitHub, Bitbucket, GitLab — or anywhere people host Git repositories. You can fork a repository through the hosted UI.
After you've forked a repository, you normally need to clone the repository to your machine. You can do some small edits on GitHub, for instance, without cloning, but this isn't a github-flight-rules list, so let's go with how to do this locally.
# if you are using ssh
$ git clone git@github.com:k88hudson/git-flight-rules.git
# if you are using https
$ git clone https://github.com/k88hudson/git-flight-rules.git
If you cd
into the resulting directory, and type git remote
, you'll see a list of the remotes. Normally there will be one remote - origin
- which will point to k88hudson/git-flight-rules
. In this case, we also want a remote that will point to your fork.
First, to follow a Git convention, we normally use the remote name origin
for your own repository and upstream
for whatever you've forked. So, rename the origin
remote to upstream
$ git remote rename origin upstream
You can also do this using git remote set-url
, but it takes longer and is more steps.
Then, set up a new remote that points to your project.
$ git remote add origin git@github.com:YourName/git-flight-rules.git
Note that now you have two remotes.
origin
references your own repository.upstream
references the original one.From origin, you can read and write. From upstream, you can only read.
When you've finished making whatever changes you like, push your changes (normally in a branch) to the remote named origin
. If you're on a branch, you could use --set-upstream
to avoid specifying the remote tracking branch on every future push using this branch. For instance:
$ (feature/my-feature) git push --set-upstream origin feature/my-feature
There is no way to suggest a pull request using the CLI using Git (although there are tools, like hub, which will do this for you). So, if you're ready to make a pull request, go to your GitHub (or another Git host) and create a new pull request. Note that your host automatically links the original and forked repositories.
After all of this, do not forget to respond to any code review feedback.
Another approach to suggesting code changes that doesn't rely on third party sites such as Github is to use git format-patch
.
format-patch
creates a .patch file for one or more commits. This file is essentially a list of changes that looks similar to the commit diffs you can view on Github.
A patch can be viewed and even edited by the recipient and applied using git am
.
For example, to create a patch based on the previous commit you would run git format-patch HEAD^
which would create a .patch file called something like 0001-My-Commit-Message.patch.
To apply this patch file to your repository you would run git am ./0001-My-Commit-Message.patch
.
Patches can also be sent via email using the git send-email
command. For information on usage and configuration see: https://git-send-email.io
After a while, the upstream
repository may have been updated, and these updates need to be pulled into your origin
repo. Remember that like you, other people are contributing too. Suppose that you are in your own feature branch and you need to update it with the original repository updates.
You probably have set up a remote that points to the original project. If not, do this now. Generally we use upstream
as a remote name:
$ (main) git remote add upstream <link-to-original-repository>
# $ (main) git remote add upstream git@github.com:k88hudson/git-flight-rules.git
Now you can fetch from upstream and get the latest updates.
$ (main) git fetch upstream
$ (main) git merge upstream/main
# or using a single command
$ (main) git pull upstream main
Let's say that you just blindly committed changes with git commit -a
and you're not sure what the actual content of the commit you just made was. You can show the latest commit on your current HEAD with:
(main)$ git show
Or
$ git log -n1 -p
If you want to see a file at a specific commit, you can also do this (where <commitid>
is the commit you're interested in):
$ git show <commitid>:filename
If you wrote the wrong thing and the commit has not yet been pushed, you can do the following to change the commit message without changing the changes in the commit:
$ git commit --amend --only
This will open your default text editor, where you can edit the message. On the other hand, you can do this all in one command:
$ git commit --amend --only -m 'xxxxxxx'
If you have already pushed the message, you can amend the commit and force push, but this is not recommended.
If it's a single commit, amend it
$ git commit --amend --no-edit --author "New Authorname <authoremail@mydomain.com>"
An alternative is to correctly configure your author settings in git config --global author.(name|email)
and then use
$ git commit --amend --reset-author --no-edit
If you need to change all of history, see the man page for git filter-branch
.
In order to remove changes for a file from the previous commit, do the following:
$ git checkout HEAD^ myfile
$ git add myfile
$ git commit --amend --no-edit
In case the file was newly added to the commit and you want to remove it (from Git alone), do:
$ git rm --cached myfile
$ git commit --amend --no-edit
This is particularly useful when you have an open patch and you have committed an unnecessary file, and need to force push to update the patch on a remote. The --no-edit
option is used to keep the existing commit message.
If you need to delete pushed commits, you can use the following. However, it will irreversibly change your history, and mess up the history of anyone else who had already pulled from the repository. In short, if you're not sure, you should never do this, ever.
$ git reset HEAD^ --hard
$ git push --force-with-lease [remote] [branch]
If you haven't pushed, to reset Git to the state it was in before you made your last commit (while keeping your staged changes):
(my-branch*)$ git reset --soft HEAD@{1}
This only works if you haven't pushed. If you have pushed, the only truly safe thing to do is git revert SHAofBadCommit
. That will create a new commit that undoes all the previous commit's changes. Or, if the branch you pushed to is rebase-safe (ie. other devs aren't expected to pull from it), you can just use git push --force-with-lease
. For more, see the above section.
The same warning applies as above. Never do this if possible.
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push --force-with-lease [remote] [branch]
Or do an interactive rebase and remove the line(s) corresponding to commit(s) you want to see removed.
To https://github.com/yourusername/repo.git
! [rejected] mybranch -> mybranch (non-fast-forward)
error: failed to push some refs to 'https://github.com/tanay1337/webmaker.org.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Note that, as with rebasing (see below), amending replaces the old commit with a new one, so you must force push (--force-with-lease
) your changes if you have already pushed the pre-amended commit to your remote. Be careful when you do this – always make sure you specify a branch!
(my-branch)$ git push origin mybranch --force-with-lease
In general, avoid force pushing. It is best to create and push a new commit rather than force-pushing the amended commit as it will cause conflicts in the source history for any other developer who has interacted with the branch in question or any child branches. --force-with-lease
will still fail, if someone else was also working on the same branch as you, and your push would overwrite those changes.
If you are absolutely sure that nobody is working on the same branch or you want to update the tip of the branch unconditionally, you can use --force
(-f
), but this should be avoided in general.
If you accidentally do git reset --hard
, you can normally still get your commit back, as git keeps a log of everything for a few days.
Note: This is only valid if your work is backed up, i.e., either committed or stashed. git reset --hard
will remove uncommitted modifications, so use it with caution. (A safer option is git reset --keep
.)
(main)$ git reflog
You'll see a list of your past commits, and a commit for the reset. Choose the SHA of the commit you want to return to, and reset again:
(main)$ git reset --hard SHA1234
And you should be good to go.
If you accidentally merged a feature branch to the main development branch before it was ready to be merged, you can still undo the merge. But there's a catch: A merge commit has more than one parent (usually two).
The command to use
(feature-branch)$ git revert -m 1 <commit>
where the -m 1 option says to select parent number 1 (the branch into which the merge was made) as the parent to revert to.
Note: the parent number is not a commit identifier. Rather, a merge commit has a line Merge: 8e2ce2d 86ac2e7
. The parent number is the 1-based index of the desired parent on this line, the first identifier is number 1, the second is number 2, and so on.
If you accidentally pushed files containing sensitive, or private data (passwords, keys, etc.), you can amend the previous commit. Keep in mind that once you have pushed a commit, you should consider any data it contains to be compromised. These steps can remove the sensitive data from your public repo or your local copy, but you cannot remove the sensitive data from other people's pulled copies. If you committed a password, change it immediately. If you committed a key, re-generate it immediately. Amending the pushed commit is not enough, since anyone could have pulled the original commit containing your sensitive data in the meantime.
If you edit the file and remove the sensitive data, then run
(feature-branch)$ git add edited_file
(feature-branch)$ git commit --amend --no-edit
(feature-branch)$ git push --force-with-lease origin [branch]
If you want to remove an entire file (but keep it locally), then run
(feature-branch)$ git rm --cached sensitive_file
echo sensitive_file >> .gitignore
(feature-branch)$ git add .gitignore
(feature-branch)$ git commit --amend --no-edit
(feature-branch)$ git push --force-with-lease origin [branch]
Alternatively store your sensitive data in local environment variables.
If you want to completely remove an entire file (and not keep it locally), then run
(feature-branch)$ git rm sensitive_file
(feature-branch)$ git commit --amend --no-edit
(feature-branch)$ git push --force-with-lease origin [branch]
If you have made other commits in the meantime (i.e. the sensitive data is in a commit before the previous commit), you will have to rebase.
If the file you want to delete is secret or sensitive, instead see how to remove sensitive files.
Even if you delete a large or unwanted file in a recent commit, it still exists in git history, in your repo's .git
folder, and will make git clone
download unneeded files.
The actions in this part of the guide will require a force push, and rewrite large sections of repo history, so if you are working with remote collaborators, check first that any local work of theirs is pushed.
There are two options for rewriting history, the built-in git-filter-branch
or bfg-repo-cleaner
. bfg
is significantly cleaner and more performant, but it is a third-party download and requires java. We will describe both alternatives. The final step is to force push your changes, which requires special consideration on top of a regular force push, given that a great deal of repo history will have been permanently changed.
Using bfg-repo-cleaner requires java. Download the bfg jar from the link here. Our examples will use bfg.jar
, but your download may have a version number, e.g. bfg-1.13.0.jar
.
To delete a specific file.
(main)$ git rm path/to/filetoremove
(main)$ git commit -m "Commit removing filetoremove"
(main)$ java -jar ~/Downloads/bfg.jar --delete-files filetoremove
Note that in bfg you must use the plain file name even if it is in a subdirectory.
You can also delete a file by pattern, e.g.:
(main)$ git rm *.jpg
(main)$ git commit -m "Commit removing *.jpg"
(main)$ java -jar ~/Downloads/bfg.jar --delete-files *.jpg
With bfg, the files that exist on your latest commit will not be affected. For example, if you had several large .tga files in your repo, and then in an earlier commit, you deleted a subset of them, this call does not touch files present in the latest commit
Note, if you renamed a file as part of a commit, e.g. if it started as LargeFileFirstName.mp4
and a commit changed it to LargeFileSecondName.mp4
, running java -jar ~/Downloads/bfg.jar --delete-files LargeFileSecondName.mp4
will not remove it from git history. Either run the --delete-files
command with both filenames, or with a matching pattern.
git-filter-branch
is more cumbersome and has less features, but you may use it if you cannot install or run bfg
.
In the below, replace filepattern
may be a specific name or pattern, e.g. *.jpg
. This will remove files matching the pattern from all history and branches.
(main)$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch filepattern' --prune-empty --tag-name-filter cat -- --all
Behind-the-scenes explanation:
--tag-name-filter cat
is a cumbersome, but simplest, way to apply the original tags to the new commits, using the command cat.
--prune-empty
removes any now-empty commits.
Once you have removed your desired files, test carefully that you haven't broken anything in your repo - if you have, it is easiest to re-clone your repo to start over. To finish, optionally use git garbage collection to minimize your local .git folder size, and then force push.
(main)$ git reflog expire --expire=now --all && git gc --prune=now --aggressive
(main)$ git push origin --force --tags
Since you just rewrote the entire git repo history, the git push
operation may be too large, and return the error “The remote end hung up unexpectedly”
. If this happens, you can try increasing the git post buffer:
(main)$ git config http.postBuffer 524288000
(main)$ git push --force
If this does not work, you will need to manually push the repo history in chunks of commits. In the command below, try increasing <number>
until the push operation succeeds.
(main)$ git push -u origin HEAD~<number>:refs/head/main --force
Once the push operation succeeds the first time, decrease <number>
gradually until a conventional git push
succeeds.
Consider you created some (e.g. three) commits and later realize you missed doing something that belongs contextually into the first of those commits. This bothers you, because if you'd create a new commit containing those changes, you'd have a clean code base, but your commits weren't atomic (i.e. changes that belonged to each other weren't in the same commit). In such a situation you may want to change the commit where these changes belong to, include them and have the following commits unaltered. In such a case, git rebase
might save you.
Consider a situation where you want to change the third last commit you made.
(your-branch)$ git rebase -i HEAD~4
gets you into interactive rebase mode, which allows you to edit any of your last three commits. A text editor pops up, showing you something like
pick 9e1d264 The third last commit
pick 4b6e19a The second to last commit
pick f4037ec The last commit
which you change into
edit 9e1d264 The third last commit
pick 4b6e19a The second to last commit
pick f4037ec The last commit
This tells rebase that you want to edit your third last commit and keep the other two unaltered. Then you'll save (and close) the editor. Git will then start to rebase. It stops on the commit you want to alter, giving you the chance to edit that commit. Now you can apply the changes which you missed applying when you initially committed that commit. You do so by editing and staging them. Afterwards you'll run
(your-branch)$ git commit --amend
which tells Git to recreate the commit, but to leave the commit message unedited. Having done that, the hard part is solved.
(your-branch)$ git rebase --continue
will do the rest of the work for you.
$ git add -u
# to stage files with ext .txt
$ git add -u *.txt
# to stage all files inside directory src
$ git add -u src/
(my-branch*)$ git commit --amend
If you already know you don't want to change the commit message, you can tell git to reuse the commit message:
(my-branch*)$ git commit --amend -C HEAD
Normally, if you want to stage part of a file, you run this:
$ git add --patch filename.x
-p
will work for short. This will open interactive mode. You would be able to use the s
option to split the commit - however, if the file is new, you will not have this option. To add a new file, do this:
$ git add -N filename.x
Then, you will need to use the e
option to manually choose which lines to add. Running git diff --cached
or git diff --staged
will show you which lines you have staged compared to which are still saved locally.
git add
will add the entire file to a commit. git add -p
will allow to interactively select which changes you want to add.
git reset -p
will open a patch mode reset dialog. This is similar to git add -p
, except that selecting "yes" will unstage the change, removing it from the upcoming commit.
In many cases, you should unstage all of your staged files and then pick the file you want and commit it. However, if you want to switch the staged and unstaged edits, you can create a temporary commit to store your staged files, stage your unstaged files and then stash them. Then, reset the temporary commit and pop your stash.
$ git commit -m "WIP"
$ git add . # This will also add untracked files.
$ git stash
$ git reset HEAD^
$ git stash pop --index 0
NOTE 1: The reason to use pop
here is want to keep idempotent as much as possible. NOTE 2: Your staged files will be marked as unstaged if you don't use the --index
flag. (This link explains why.)
$ git checkout -b my-branch
$ git stash
$ git checkout my-branch
$ git stash pop
If you want to discard all your local staged and unstaged changes, you can do this:
(my-branch)$ git reset --hard
# or
(main)$ git checkout -f
This will unstage all files you might have staged with git add
:
$ git reset
This will revert all local uncommitted changes (should be executed in repo root):
$ git checkout .
You can also revert uncommitted changes to a particular file or directory:
$ git checkout [some_dir|file.txt]
Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):
$ git reset --hard HEAD
This will remove all local untracked files, so only files tracked by Git remain:
$ git clean -fd
-x
will also remove all ignored files.
When you want to get rid of some, but not all changes in your working copy.
Checkout undesired changes, keep good changes.
$ git checkout -p
# Answer y to all of the snippets you want to drop
Another strategy involves using stash
. Stash all the good changes, reset working copy, and reapply good changes.
$ git stash -p
# Select all of the snippets you want to save
$ git reset --hard
$ git stash pop
Alternatively, stash your undesired changes, and then drop stash.
$ git stash -p
# Select all of the snippets you don't want to save
$ git stash drop
When you want to get rid of one specific file in your working copy.
$ git checkout myFile
Alternatively, to discard multiple files in your working copy, list them all.
$ git checkout myFirstFile mySecondFile
When you want to get rid of all of your unstaged local uncommitted changes
$ git checkout .
When you want to get rid of all of your untracked files
$ git clean -f
Sometimes we have one or more files that accidentally ended up being staged, and these files have not been committed before. To unstage them:
$ git reset -- <filename>
This results in unstaging the file and make it look like it's untracked.
List local branches
$ git branch
List remote branches
$ git branch -r
List all branches (both local and remote)
$ git branch -a
$ git checkout -b <branch> <SHA1_OF_COMMIT>
This is another chance to use git reflog
to see where your HEAD pointed before the bad pull.
(main)$ git reflog
ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward
c5bc55a HEAD@{1}: checkout: checkout message goes here
Simply reset your branch back to the desired commit:
$ git reset --hard c5bc55a
Done.
Confirm that you haven't pushed your changes to the server.
git status
should show how many commits you are ahead of origin:
(my-branch)$ git status
# On branch my-branch
# Your branch is ahead of 'origin/my-branch' by 2 commits.
# (use "git push" to publish your local commits)
#
One way of resetting to match origin (to have the same as what is on the remote) is to do this:
(main)$ git reset --hard origin/my-branch
Create the new branch while remaining on main:
(main)$ git branch my-branch
Reset the branch main to the previous commit:
(main)$ git reset --hard HEAD^
HEAD^
is short for HEAD^1
. This stands for the first parent of HEAD
, similarly HEAD^2
stands for the second parent of the commit (merges can have 2 parents).
Note that HEAD^2
is not the same as HEAD~2
(see this link for more information).
Alternatively, if you don't want to use HEAD^
, find out what the commit hash you want to set your main branch to (git log
should do the trick). Then reset to that hash. git push
will make sure that this change is reflected on your remote.
For example, if the hash of the commit that your main branch is supposed to be at is a13b85e
:
(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
Checkout the new branch to continue working:
(main)$ git checkout my-branch
Say you have a working spike (see note), with hundreds of changes. Everything is working. Now, you commit into another branch to save that work:
(solution)$ git add -A && git commit -m "Adding all changes from this spike into one big commit."
When you want to put it into a branch (maybe feature, maybe develop
), you're interested in keeping whole files. You want to split your big commit into smaller ones.
Say you have:
solution
, with the solution to your spike. One ahead of develop
.develop
, where you want to add your changes.You can solve it bringing the contents to your branch:
(develop)$ git checkout solution -- file1.txt
This will get the contents of that file in branch solution
to your branch develop
:
# On branch develop
# Your branch is up-to-date with 'origin/develop'.
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file1.txt
Then, commit as usual.
Note: Spike solutions are made to analyze or solve the problem. These solutions are used for estimation and discarded once everyone gets clear visualization of the problem. ~ Wikipedia.
Say you are on your main branch. Running git log
, you see you have made two commits:
(main)$ git log
commit e3851e817c451cc36f2e6f3049db528415e3c114
Author: Alex Lee <alexlee@example.com>
Date: Tue Jul 22 15:39:27 2014 -0400
Bug #21 - Added CSRF protection
commit 5ea51731d150f7ddc4a365437931cd8be3bf3131
Author: Alex Lee <alexlee@example.com>
Date: Tue Jul 22 15:39:12 2014 -0400
Bug #14 - Fixed spacing on title
commit a13b85e984171c6e2a1729bb061994525f626d14
Author: Aki Rose <akirose@example.com>
Date: Tue Jul 21 01:12:48 2014 -0400
First commit
Let's take note of our commit hashes for each bug (e3851e8
for #21, 5ea5173
for #14).
First, let's reset our main branch to the correct commit (a13b85e
):
(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
Now, we can create a fresh branch for our bug #21:
(main)$ git checkout -b 21
(21)$
Now, let's cherry-pick the commit for bug #21 on top of our branch. That means we will be applying that commit, and only that commit, directly on top of whatever our head is at.
(21)$ git cherry-pick e3851e8
At this point, there is a possibility there might be conflicts. See the There were conflicts section in the interactive rebasing section above for how to resolve conflicts.
Now let's create a new branch for bug #14, also based on main
(21)$ git checkout main
(main)$ git checkout -b 14
(14)$
And finally, let's cherry-pick the commit for bug #14:
(14)$ git cherry-pick 5ea5173
Once you merge a pull request on GitHub, it gives you the option to delete the merged branch in your fork. If you aren't planning to keep working on the branch, it's cleaner to delete the local copies of the branch so you don't end up cluttering up your working checkout with a lot of stale branches.
$ git fetch -p upstream
where, upstream
is the remote you want to fetch from.
If you're regularly pushing to remote, you should be safe most of the time. But still sometimes you may end up deleting your branches. Let's say we create a branch and create a new file:
(main)$ git checkout -b my-branch
(my-branch)$ git branch
(my-branch)$ touch foo.txt
(my-branch)$ ls
README.md foo.txt
Let's add it and commit.
(my-branch)$ git add .
(my-branch)$ git commit -m 'foo.txt added'
(my-branch)$ foo.txt added
1 files changed, 1 insertions(+)
create mode 100644 foo.txt
(my-branch)$ git log
commit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012
Author: siemiatj <siemiatj@example.com>
Date: Wed Jul 30 00:34:10 2014 +0200
foo.txt added
commit 69204cdf0acbab201619d95ad8295928e7f411d5
Author: Kate Hudson <katehudson@example.com>
Date: Tue Jul 29 13:14:46 2014 -0400
Fixes #6: Force pushing after amending commits
Now we're switching back to main and 'accidentally' removing our branch.
(my-branch)$ git checkout main
Switched to branch 'main'
Your branch is up-to-date with 'origin/main'.
(main)$ git branch -D my-branch
Deleted branch my-branch (was 4e3cd85).
(main)$ echo oh noes, deleted my branch!
oh noes, deleted my branch!
At this point you should get familiar with 'reflog', an upgraded logger. It stores the history of all the action in the repo.
(main)$ git reflog
69204cd HEAD@{0}: checkout: moving from my-branch to main
4e3cd85 HEAD@{1}: commit: foo.txt added
69204cd HEAD@{2}: checkout: moving from main to my-branch
As you can see we have commit hash from our deleted branch. Let's see if we can restore our deleted branch.
(main)$ git checkout -b my-branch-help
Switched to a new branch 'my-branch-help'
(my-branch-help)$ git reset --hard 4e3cd85
HEAD is now at 4e3cd85 foo.txt added
(my-branch-help)$ ls
README.md foo.txt
Voila! We got our removed file back. git reflog
is also useful when rebasing goes terribly wrong.
To delete a remote branch:
(main)$ git push origin --delete my-branch
You can also do:
(main)$ git push origin :my-branch
To delete a local branch:
(main)$ git branch -d my-branch
To delete a local branch that has not been merged to the current branch or an upstream:
(main)$ git branch -D my-branch
Say you want to delete all branches that start with fix/
:
(main)$ git branch | grep 'fix/' | xargs git branch -d
To rename the current (local) branch:
(main)$ git branch -m new-name
To rename a different (local) branch:
(main)$ git branch -m old-name new-name
To delete the old-name
remote branch and push the new-name
local branch:
(main)$ git push origin :old_name new_name
First, fetch all branches from remote:
(main)$ git fetch --all
Say you want to checkout to daves
from the remote.
(main)$ git checkout --track origin/daves
Branch daves set up to track remote branch daves from origin.
Switched to a new branch 'daves'
(--track
is shorthand for git checkout -b [branch] [remotename]/[branch]
)
This will give you a local copy of the branch daves
, and any update that has been pushed will also show up remotely.
$ git push <remote> HEAD
If you would also like to set that remote branch as upstream for the current one, use the following instead:
$ git push -u <remote> HEAD
With the upstream
mode and the simple
(default in Git 2.0) mode of the push.default
config, the following command will push the current branch with regards to the remote branch that has been registered previously with -u
:
$ git push
The behavior of the other modes of git push
is described in the doc of push.default
.
You can set a remote branch as the upstream for the current local branch using:
$ git branch --set-upstream-to [remotename]/[branch]
# or, using the shorthand:
$ git branch -u [remotename]/[branch]
To set the upstream remote branch for another local branch:
$ git branch -u [remotename]/[branch] [local-branch]
By checking your remote branches, you can see which remote branch your HEAD is tracking. In some cases, this is not the desired branch.
$ git branch -r
origin/HEAD -> origin/gh-pages
origin/main
To change origin/HEAD
to track origin/main
, you can run this command:
$ git remote set-head origin --auto
origin/HEAD set to main
You've made uncommitted changes and realise you're on the wrong branch. Stash changes and apply them to the branch you want:
(wrong_branch)$ git stash
(wrong_branch)$ git checkout <correct_branch>
(correct_branch)$ git stash apply
You've made a lot of commits on a branch and now want to separate it into two, ending with a branch up to an earlier commit and another with all the changes.
Use git log
to find the commit where you want to split. Then do the following:
(original_branch)$ git checkout -b new_branch
(new_branch)$ git checkout original_branch
(original_branch)$ git reset --hard <sha1 split here>
If you had previously pushed the original_branch
to remote, you will need to do a force push. For more information check Stack Overlflow
You may have merged or rebased your current branch with a wrong branch, or you can't figure it out or finish the rebase/merge process. Git saves the original HEAD pointer in a variable called ORIG_HEAD before doing dangerous operations, so it is simple to recover your branch at the state before the rebase/merge.
(my-branch)$ git reset --hard ORIG_HEAD
Unfortunately, you have to force push, if you want those changes to be reflected on the remote branch. This is because you have changed the history. The remote branch won't accept changes unless you force push. This is one of the main reasons many people use a merge workflow, instead of a rebasing workflow - large teams can get into trouble with developers force pushing. Use this with caution. A safer way to use rebase is not to reflect your changes on the remote branch at all, and instead to do the following:
(main)$ git checkout my-branch
(my-branch)$ git rebase -i main
(my-branch)$ git checkout main
(main)$ git merge --ff-only my-branch
For more, see this SO thread.
Let's suppose you are working in a branch that is/will become a pull-request against main
. In the simplest case when all you want to do is to combine all commits into a single one and you don't care about commit timestamps, you can reset and recommit. Make sure the main branch is up to date and all your changes committed, then:
(my-branch)$ git reset --soft main
(my-branch)$ git commit -am "New awesome feature"
If you want more control, and also to preserve timestamps, you need to do something called an interactive rebase:
(my-branch)$ git rebase -i main
If you aren't working against another branch you'll have to rebase relative to your HEAD
. If you want to squash the last 2 commits, for example, you'll have to rebase against HEAD~2
. For the last 3, HEAD~3
, etc.
(main)$ git rebase -i HEAD~2
After you run the interactive rebase command, you will see something like this in your text editor:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
pick b729ad5 fixup
pick e3851e8 another fix
# Rebase 8074d12..b729ad5 onto 8074d12
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
All the lines beginning with a #
are comments, they won't affect your rebase.
Then you replace pick
commands with any in the list above, and you can also remove commits by removing corresponding lines.
For example, if you want to leave the oldest (first) commit alone and combine all the following commits with the second oldest, you should edit the letter next to each commit except the first and the second to say f
:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
f b729ad5 fixup
f e3851e8 another fix
If you want to combine these commits and rename the commit, you should additionally add an r
next to the second commit or simply use s
instead of f
:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
s b729ad5 fixup
s e3851e8 another fix
You can then rename the commit in the next text prompt that pops up.
Newer, awesomer features
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 8074d12
# You are currently editing a commit while rebasing branch 'main' on '8074d12'.
#
# Changes to be committed:
# modified: README.md
#
If everything is successful, you should see something like this:
(main)$ Successfully rebased and updated refs/heads/main.
--no-commit
performs the merge but pretends the merge failed and does not autocommit, giving the user a chance to inspect and further tweak the merge result before committing. no-ff
maintains evidence that a feature branch once existed, keeping project history consistent.
(main)$ git merge --no-ff --no-commit my-branch
(main)$ git merge --squash my-branch
Sometimes you have several work in progress commits that you want to combine before you push them upstream. You don't want to accidentally combine any commits that have already been pushed upstream because someone else may have already made commits that reference them.
(main)$ git rebase -i @{u}
This will do an interactive rebase that lists only the commits that you haven't already pushed, so it will be safe to reorder/fix/squash anything in the list.
Sometimes the merge can produce problems in certain files, in those cases we can use the option abort
to abort the current conflict resolution process, and try to reconstruct the pre-merge state.
(my-branch)$ git merge --abort
This command is available since Git version >= 1.7.4
Say I have a main branch, a feature-1 branch branched from main, and a feature-2 branch branched off of feature-1. If I make a commit to feature-1, then the parent commit of feature-2 is no longer accurate (it should be the head of feature-1, since we branched off of it). We can fix this with git rebase --onto
.
(feature-2)$ git rebase --onto feature-1 <the first commit in your feature-2 branch that you don't want to bring along> feature-2
This helps in sticky scenarios where you might have a feature built on another feature that hasn't been merged yet, and a bugfix on the feature-1 branch needs to be reflected in your feature-2 branch.
To check if all commits on a branch are merged into another branch, you should diff between the heads (or any commits) of those branches:
(main)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll
This will tell you if any commits are in one but not the other, and will give you a list of any nonshared between the branches. Another option is to do this:
(main)$ git log main ^feature/120-on-scroll --no-merges
If you're seeing this:
noop
That means you are trying to rebase against a branch that is at an identical commit, or is ahead of your current branch. You can try:
HEAD~2
or earlier instead
If you are unable to successfully complete the rebase, you may have to resolve conflicts.
First run git status
to see which files have conflicts in them:
(my-branch)$ git status
On branch my-branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
both modified: README.md
In this example, README.md
has conflicts. Open that file and look for the following:
<<<<<<< HEAD
some code
=========
some code
>>>>>>> new-commit
You will need to resolve the differences between the code that was added in your new commit (in the example, everything from the middle line to new-commit
) and your HEAD
.
If you want to keep one branch's version of the code, you can use --ours
or --theirs
:
(main*)$ git checkout --ours README.md
--ours
to keep changes from the local branch, or --theirs
to keep changes from the other branch.--theirs
to keep changes from the local branch, or --ours
to keep changes from the other branch. For an explanation of this swap, see this note in the Git documentation.If the merges are more complicated, you can use a visual diff editor:
(main*)$ git mergetool -t opendiff
After you have resolved all conflicts and tested your code, git add
the files you have changed, and then continue the rebase with git rebase --continue
(my-branch)$ git add README.md
(my-branch)$ git rebase --continue
If after resolving all the conflicts you end up with an identical tree to what it was before the commit, you need to git rebase --skip
instead.
If at any time you want to stop the entire rebase and go back to the original state of your branch, you can do so:
(my-branch)$ git rebase --abort
To stash all the edits in your working directory
$ git stash
If you also want to stash untracked files, use -u
option.
$ git stash -u
To stash only one file from your working directory
$ git stash push working-directory-path/filename.ext
To stash multiple files from your working directory
$ git stash push working-directory-path/filename1.ext working-directory-path/filename2.ext
$ git stash save <message>
or
$ git stash push -m <message>
First check your list of stashes with message using
$ git stash list
Then apply a specific stash from the list using
$ git stash apply "stash@{n}"
Here, 'n' indicates the position of the stash in the stack. The topmost stash will be position 0.
Furthermore, using a time-based stash reference is also possible.
$ git stash apply "stash@{2.hours.ago}"
You can manually create a stash commit
, and then use git stash store
.
$ git stash create
$ git stash store -m <message> CREATED_SHA1
To find a certain string which was introduced in any commit, you can use the following structure:
$ git log -S "string to find"
Commons parameters:
--source
means to show the ref name given on the command line by which each commit was reached.
--all
means to start from every branch.
--reverse
prints in reverse order, it means that will show the first commit that made the change.
To find all commits by author/committer you can use:
$ git log --author=<name or email>
$ git log --committer=<name or email>
Keep in mind that author and committer are not the same. The --author
is the person who originally wrote the code; on the other hand, the --committer
, is the person who committed the code on behalf of the original author.
To find all commits containing a specific file you can use:
$ git log -- <path to file>
You would usually specify an exact path, but you may also use wild cards in the path and file name:
$ git log -- **/*.js
While using wildcards, it's useful to inform --name-status
to see the list of committed files:
$ git log --name-status -- **/*.js
To trace the evolution of a single function you can use:
$ git log -L :FunctionName:FilePath
Note that you can combine this with further git log
options, like revision ranges and commit limits.
To find all tags containing a specific commit:
$ git tag --contains <commitid>
$ git clone --recursive git://github.com/foo/bar.git
If already cloned:
$ git submodule update --init --recursive
Creating a submodule is pretty straight-forward, but deleting them less so. The commands you need are:
$ git submodule deinit submodulename
$ git rm submodulename
$ git rm --cached submodulename
$ rm -rf .git/modules/submodulename
$ git checkout <branch-you-want-the-directory-from> -- <folder-name or file-name>
First find the commit when the file last existed:
$ git rev-list -n 1 HEAD -- filename
Then checkout that file:
git checkout deletingcommitid^ -- filename
$ git tag -d <tag_name>
$ git push <remote> :refs/tags/<tag_name>
If you want to recover a tag that was already deleted, you can do so by following these steps: First, you need to find the unreachable tag:
$ git fsck --unreachable | grep tag
Make a note of the tag's hash. Then, restore the deleted tag with following, making use of git update-ref
:
$ git update-ref refs/tags/<tag_name> <hash>
Your tag should now have been restored.
If someone has sent you a pull request on GitHub, but then deleted their original fork, you will be unable to clone their repository or to use git am
as the .diff, .patch URLs become unavailable. But you can checkout the PR itself using GitHub's special refs. To fetch the content of PR#1 into a new branch called pr_1:
$ git fetch origin refs/pull/1/head:pr_1
From github.com:foo/bar
* [new ref] refs/pull/1/head -> pr_1
$ git archive --format zip --output /full/path/to/zipfile.zip main
If there is a tag on a remote repository that has the same name as a branch you will get the following error when trying to push that branch with a standard $ git push <remote> <branch>
command.
$ git push origin <branch>
error: dst refspec same matches more than one.
error: failed to push some refs to '<git server>'
Fix this by specifying you want to push the head reference.
$ git push origin refs/heads/<branch-name>
If you want to push a tag to a remote repository that has the same name as a branch, you can use a similar command.
$ git push origin refs/tags/<tag-name>
(main)$ git mv --force myfile MyFile
(main)$ git fetch --all
(main)$ git reset --hard origin/main
(main)$ git rm --cached log.txt
Assuming the hash of the commit you want is c5f567:
(main)$ git checkout c5f567 -- file1/to/restore file2/to/restore
If you want to revert to changes made just 1 commit before c5f567, pass the commit hash as c5f567~1:
(main)$ git checkout c5f567~1 -- file1/to/restore file2/to/restore
Assuming you want to compare last commit with file from commit c5f567:
$ git diff HEAD:path_to_file/file c5f567:path_to_file/file
Same goes for branches:
$ git diff main:path_to_file/file staging:path_to_file/file
This works great for config templates or other files that require locally adding credentials that shouldn't be committed.
$ git update-index --assume-unchanged file-to-ignore
Note that this does not remove the file from source control - it is only ignored locally. To undo this and tell Git to notice changes again, this clears the ignore flag:
$ git update-index --no-assume-unchanged file-to-stop-ignoring
The git-bisect command uses a binary search to find which commit in your Git history introduced a bug.
Suppose you're on the main
branch, and you want to find the commit that broke some feature. You start bisect:
$ git bisect start
Then you should specify which commit is bad, and which one is known to be good. Assuming that your current version is bad, and v1.1.1
is good:
$ git bisect bad
$ git bisect good v1.1.1
Now git-bisect
selects a commit in the middle of the range that you specified, checks it out, and asks you whether it's good or bad. You should see something like:
$ Bisecting: 5 revision left to test after this (roughly 5 step)
$ [c44abbbee29cb93d8499283101fe7c8d9d97f0fe] Commit message
$ (c44abbb)$
You will now check if this commit is good or bad. If it's good:
$ (c44abbb)$ git bisect good
and git-bisect
will select another commit from the range for you. This process (selecting good
or bad
) will repeat until there are no more revisions left to inspect, and the command will finally print a description of the first bad commit.
On OS X and Linux, your git configuration file is stored in ~/.gitconfig
. I've added some example aliases I use as shortcuts (and some of my common typos) in the [alias]
section as shown below:
[alias]
a = add
amend = commit --amend
c = commit
ca = commit --amend
ci = commit -a
co = checkout
d = diff
dc = diff --changed
ds = diff --staged
extend = commit --amend -C HEAD
f = fetch
loll = log --graph --decorate --pretty=oneline --abbrev-commit
m = merge
one = log --pretty=oneline
outstanding = rebase -i @{u}
reword = commit --amend --only
s = status
unpushed = log @{u}
wc = whatchanged
wip = rebase -i @{u}
zap = fetch -p
day = log --reverse --no-merges --branches=* --date=local --since=midnight --author=\"$(git config --get user.name)\"
delete-merged-branches = "!f() { git checkout --quiet main && git branch --merged | grep --invert-match '\\*' | xargs -n 1 git branch --delete; git checkout --quiet @{-1}; }; f"
You can’t! Git doesn’t support this, but there’s a hack. You can create a .gitignore file in the directory with the following contents:
# Ignore everything in this directory
*
# Except this file
!.gitignore
Another common convention is to make an empty file in the folder, titled .gitkeep.
$ mkdir mydir
$ touch mydir/.gitkeep
You can also name the file as just .keep , in which case the second line above would be touch mydir/.keep
You might have a repository that requires authentication. In which case you can cache a username and password so you don't have to enter it on every push and pull. Credential helper can do this for you.
$ git config --global credential.helper cache
# Set git to use the credential memory cache
$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)
To find a credential helper:
$ git help -a | grep credential
# Shows you possible credential helpers
For OS specific credential caching:
$ git config --global credential.helper osxkeychain
# For OSX
$ git config --global credential.helper manager
# Git for Windows 2.7.3+
$ git config --global credential.helper gnome-keyring
# Ubuntu and other GNOME-based distros
More credential helpers can likely be found for different distributions and operating systems.
$ git config core.fileMode false
If you want to make this the default behaviour for logged-in users, then use:
$ git config --global core.fileMode false
To configure user information used across all local repositories, and to set a name that is identifiable for credit when review version history:
$ git config --global user.name “[firstname lastname]”
To set an email address that will be associated with each history marker:
git config --global user.email “[valid-email]”
So, you're screwed - you reset
something, or you merged the wrong branch, or you force pushed and now you can't find your commits. You know, at some point, you were doing alright, and you want to go back to some state you were at.
This is what git reflog
is for. reflog
keeps track of any changes to the tip of a branch, even if that tip isn't referenced by a branch or a tag. Basically, every time HEAD changes, a new entry is added to the reflog. This only works for local repositories, sadly, and it only tracks movements (not changes to a file that weren't recorded anywhere, for instance).
(main)$ git reflog
0a2e358 HEAD@{0}: reset: moving to HEAD~2
0254ea7 HEAD@{1}: checkout: moving from 2.2 to main
c10f740 HEAD@{2}: checkout: moving from main to 2.2
The reflog above shows a checkout from main to the 2.2 branch and back. From there, there's a hard reset to an older commit. The latest activity is represented at the top labeled HEAD@{0}
.
If it turns out that you accidentally moved back, the reflog will contain the commit main pointed to (0254ea7) before you accidentally dropped 2 commits.
$ git reset --hard 0254ea7
Using git reset
it is then possible to change main back to the commit it was before. This provides a safety net in case history was accidentally changed.
(copied and edited from Source).
Once you're comfortable with what the above commands are doing, you might want to create some shortcuts for Git Bash. This allows you to work a lot faster by doing complex tasks in really short commands.
alias sq=squash
function squash() {
git rebase -i HEAD~$1
}
Copy those commands to your .bashrc or .bash_profile.
If you are using PowerShell on Windows, you can also set up aliases and functions. Add these commands to your profile, whose path is defined in the $profile
variable. Learn more at the About Profiles page on the Microsoft documentation site.
Set-Alias sq Squash-Commits
function Squash-Commits {
git rebase -i HEAD~$1
}
Other Resources
Author: K88hudson
Source Code: https://github.com/k88hudson/git-flight-rules
License: CC-BY-SA-4.0 License